//           mainwindow.cc
//  Fri Jan  5 14:07:41 2007
//  Copyright  2007  Min Seok Seo
//  <msseo@oasyss.co.kr>

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with main.c; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA

#include "mainwindow.h"
#include <iostream>
#include <gtkmm.h>

using namespace PGSTD;
using namespace pqxx;
using namespace std;
using namespace booyopos;

MainWindow::MainWindow(std::string get_sosok, std::string get_userid)
{
	// 장치 연결
    CashDrawer.Open("VCB-355S");
    POSPrinter.Open("TM-T88IV");

	// 변수초기화
	sales.Timer();
	sales.Init(get_sosok, get_userid);
	sales.Ujinfo();
	sales.Total_Person_Func();
	sales.Total_Price_Func();
	// 윈도우 초기화
	Init();
	// DB 접속
	connection C(sales.strConn);
	// 타이머
	Glib::signal_timeout().connect( sigc::mem_fun(*this, &MainWindow::onSecondElapsed), 1000);
	
//******************************************************************************
//   윈도우 시작
//******************************************************************************
	// 테이블 불러오기
	table_show(0);
	// 금액란 라벨 초기화
	price_info();
}


MainWindow::~MainWindow()
{
	if(m_pMainWindowMap)
	{
		delete m_pMainWindowMap;
		m_pMainWindowMap = 0;
	}
	// DB 접속 종료
	C.disconnect();
	// 장치 종료
	CashDrawer.Close();
	POSPrinter.Close();
}
//*****************************************************************************
//
//		MainWindow 초기화
//
//*****************************************************************************
void MainWindow::Init()
{
	// RC 파일 설정
	Gtk::RC("./rc/mainwindowrc");
	// 윈도우 설정
  	set_title ("리눅스 Point Of Sales");
  	set_border_width(0);
  	set_default_size(1024,768);
	//fullscreen();
  	
  	// 컬러 설정
  	Gdk::Color color;
	color.set_red(65535);
	color.set_green(0);
	color.set_blue(0);

	// 글래이드 파일 불러오기
	m_refGlade = Gnome::Glade::Xml::create("./glade/linuxpos.glade");
	m_refGlade->reparent_widget("vbox", *this);	
	m_pMainWindowMap = new Gnome::Glade::VariablesMap(m_refGlade);

	// 상단 라벨 불러오기 
	m_refGlade->get_widget("Label_Ujname",m_Label_Ujname);
	m_refGlade->get_widget("Label_Workdate",m_Label_Workdate);
	m_refGlade->get_widget("Label_Nowdate",m_Label_Nowdate);
	m_refGlade->get_widget("Label_User",m_Label_User);
	m_refGlade->get_widget("Label_Time",m_Label_Time);
	m_refGlade->get_widget("Label_Ver",m_Label_Ver);
	m_refGlade->get_widget("Label_Total_Person",m_Label_Total_Person);
	m_refGlade->get_widget("Label_Total_Price",m_Label_Total_Price);
	
	// 폰트 색 변경 테스트
	m_Label_Ver->modify_fg(Gtk::STATE_NORMAL, color);	

	// 상단 라벨 초기값 설정
    m_Label_Ujname->set_text(sales.Ujname);
    m_Label_User->set_text(sales.Username);
	m_Label_Time->set_text(sales.Time);
	m_Label_Workdate->set_text(sales.Workdate);
	m_Label_Nowdate->set_text(sales.Nowdate);
    m_Label_Ver->set_text(sales.Ver);
	m_Label_Total_Person->set_text(sales.Total_Person);
	m_Label_Total_Price->set_text(sales.Total_Price);
	
	// 중단 테이블 값 받기
	m_refGlade->get_widget("Table_Table",m_Table);  
	
	// 하단 테이블 값 받기
	m_refGlade->get_widget("Table_Low_1",m_Table_Low[0]);  	
	m_refGlade->get_widget("Table_Low_2",m_Table_Low[1]);
	
	// 세부 금액 테이블 값 받기
	m_refGlade->get_widget("Table_Price_L",m_Table_Price_L);  	
	m_refGlade->get_widget("Table_Price_R",m_Table_Price_R);
	
	// 메뉴 항목 트리 뷰 생성void
	m_refListStore = Gtk::ListStore::create(m_columns);	
	m_refListStore_Detail = Gtk::ListStore::create(m_detail_columns);	
  	// 메뉴 항목 셋 모드
	m_refGlade->get_widget("TreeView_Menu",m_TreeView_Menu);
	m_refGlade->get_widget("TreeView_Detail",m_TreeView_Detail);
	m_TreeView_Menu->set_model(m_refListStore);
	m_TreeView_Detail->set_model(m_refListStore_Detail);
	// 메뉴 항목 줄 색 변경
  	m_TreeView_Menu->set_rules_hint();
	m_TreeView_Detail->set_rules_hint();
   	// 트리뷰 컬럼 제목 설정
   	m_TreeView_Menu->append_column("               항       목                ", m_columns.product);	
   	m_TreeView_Menu->append_column("  수  량 ", m_columns.number);
  	m_TreeView_Menu->append_column("   금  액       ", m_columns.price);
	m_TreeView_Detail->append_column("   분  류   ", m_detail_columns.text);	
	m_TreeView_Detail->append_column("      내       용           ", m_detail_columns.contents);	
	
	// 중단 하단 버튼 & 세부 항목 라벨 초기화
	for(int j = 0; j < 8; j++)	// 세로 8개
  	{
 		 m_Label_Price_info[j] = NULL;	// 세부 금액 라벨 8개
    	 for(int i = 0; i < 5; i++)	// 가로 5개
     	{ 
	           m_Button_Table[i][j] = NULL;
	           m_Button_Menu[i][j] = NULL;
	           m_Button_Pay[i][j] = NULL;
	           if(j < 2) {	
	           		// 하단 버튼 (5X2)
	           		m_Button_Table_Low[i][j] = NULL;	
			 		m_Button_Menu_Low[i][j] = NULL;
			 		m_Button_Pay_Low[i][j] = NULL;
	           }
	    }
	}
	// 윈도우 현재 상태 (테이블, 메뉴, 계산)
	windowstatus = "table";
	// 테이블 현재 상태 (이체, 변경, 합산)
	tablestatus = "normal";
	menustatus = "normal";
	// 메인 윈도우 기본 버튼 이벤트 설정 (종료, 주문, 계산, 자세히, 변경)
	m_refGlade->connect_clicked("Button_Price_Detail", sigc::mem_fun(*this, &MainWindow::on_Button_Detail_clicked));
	m_refGlade->connect_clicked("Button_Exit", sigc::mem_fun(*this, &MainWindow::on_Button_Exit_clicked));
	m_refGlade->connect_clicked("Button_Table", sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &MainWindow::on_Button_Table_clicked ), "테이블"));
	m_refGlade->connect_clicked("Button_Menu", sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &MainWindow::on_Button_Menuadd_clicked ), "주문"));
	m_refGlade->connect_clicked("Button_Pay", sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &MainWindow::on_Button_Pay_clicked ), "계산"));	
	// 트리뷰 메뉴 옆 버튼
	m_refGlade->connect_clicked("Button_Menu_add", sigc::mem_fun(*this, &MainWindow::on_Button_Menu_add_clicked));
	m_refGlade->connect_clicked("Button_Menu_remove", sigc::mem_fun(*this, &MainWindow::on_Button_Menu_remove_clicked));
	m_refGlade->connect_clicked("Button_Menu_up", sigc::mem_fun(*this, &MainWindow::on_Button_Menu_up_clicked));
	m_refGlade->connect_clicked("Button_Menu_down", sigc::mem_fun(*this, &MainWindow::on_Button_Menu_down_clicked));
	
	//**************************************************************************
	//		하부 윈도우 초기화
	//**************************************************************************
	// 현금 계산 화면 초기화
	cashwindow_init();
	// 카드 계산 화면 초기화
	cardwindow_init();
	// 테이블이체화면 초기화
	tabletranswindow_init();
	// 메뉴 찾기 화면 초기화
	menufindwindow_init();
	// 빌찾기 화면 초기화
	billfindwindow_init();
	// 빌정보
	billwindow_init();
	// void 메모
	voidmemo_init();
	// 마감 로긴화면 초기화
	workclosewindow_init();
	// 메세지 윈도우 초기화
	msgwindow_init();
	// 보고서 윈도우 초기화
	reportwindow_init();
	// 메뉴 자세히 버튼 초기화
	menudetailwindow_init();
	// 수량 수정 화면 초기화
	cntwindow_init();
}
//*****************************************************************************
//
//		버튼 관련 RELOAD 함수들
//
//*****************************************************************************
// 상단버튼
void MainWindow::initbutton()
{
  	for(int j = 0; j < 8; j++)	
  	{
    	 for(int i = 0; i < 5; i++)	
     	{ 
  		 	if(m_Button_Table[i][j] != NULL) { 
    			m_Button_Table[i][j]->hide_all(); 
    			delete m_Button_Table[i][j];
    			m_Button_Table[i][j] = NULL;
   			}
 			else if(m_Button_Pay[i][j] != NULL) { 
    			m_Button_Pay[i][j]->hide_all();
    			delete m_Button_Pay[i][j];
    			m_Button_Pay[i][j] = NULL;  
    		}
    		else if(m_Button_Menu[i][j] != NULL) { 
    			m_Button_Menu[i][j]->hide_all();
    			delete m_Button_Menu[i][j];
    			m_Button_Menu[i][j] = NULL;  
   			}
   		}
   	} 				
}
// 하단버튼
void MainWindow::initlowbutton()
{
  	for(int j = 0; j < 2; j++)	// 세로 2개
  	{
    	 for(int i = 0; i < 5; i++)	// 가로 5개
     	{    
			if(m_Button_Table_Low[i][j] != NULL) {
    			m_Button_Table_Low[i][j]->hide_all();
    			delete m_Button_Table_Low[i][j];
    			m_Button_Table_Low[i][j] = NULL;
			}
 		     else if(m_Button_Pay_Low[i][j] != NULL) {
    			m_Button_Pay_Low[i][j]->hide_all();  
     			delete m_Button_Pay_Low[i][j];
    			m_Button_Pay_Low[i][j] = NULL;
  		    }
  		 	 else if(m_Button_Menu_Low[i][j] != NULL) {
   		   		m_Button_Menu_Low[i][j]->hide_all();
    			delete m_Button_Menu_Low[i][j];
  		  		m_Button_Menu_Low[i][j] = NULL;
		  	}
		}
	}
}

//*****************************************************************************
//
//	테이블 화면 관련 함수
//
//*****************************************************************************
// 테이블 보여주기
void MainWindow::table_show(int page)
{
	initbutton();
	Gdk::Color color, color2;
	color.set("#007700");
	color2.set("#00AA00");
	work T(C, "table");
	SQL=  " SELECT \
				CHEK_TRAN_ACNO,	\
				TABL_CODE_NAME, \
				TABL_CODE_TBNO, \
				CASH_BILL_BILL, \
				TABL_CODE_SORT, \
				SUM(CASH_TRAN_CHARGE-CASH_TRAN_CREDIT) \
			FROM P_TABL_CODE \
			LEFT JOIN P_CHEK_TRAN \
				ON  TABL_CODE_SOSOK	= '"+sales.SOSOK+"'\
				AND TABL_CODE_UJCD	= '"+sales.Ujcd+"'\
				AND CHEK_TRAN_DATE	= '"+sales.Workdate+"'\
				AND CHEK_TRAN_STAU  = '"+sales.CHKINCODE+"'\
				AND TABL_CODE_SOSOK	= CHEK_TRAN_SOSOK \
				AND TABL_CODE_TBNO	= CHEK_TRAN_TBNO \
				AND TABL_CODE_UJCD	= CHEK_TRAN_UJCD \
			LEFT JOIN P_CASH_BILL \
				ON  CHEK_TRAN_SOSOK	= CASH_BILL_SOSOK \
				AND CHEK_TRAN_UJCD	= CASH_BILL_UJCD \
				AND CHEK_TRAN_DATE	= CASH_BILL_DATE \
				AND CHEK_TRAN_ACNO	= CASH_BILL_ACNO \
			LEFT JOIN P_CASH_TRAN \
				ON  CHEK_TRAN_SOSOK	= CASH_TRAN_SOSOK \
				AND CHEK_TRAN_UJCD	= CASH_TRAN_UJCD \
				AND CHEK_TRAN_DATE	= CASH_TRAN_DATE \
				AND CHEK_TRAN_ACNO	= CASH_TRAN_ACNO \
				AND CASH_BILL_BILL	= CASH_TRAN_BILL \
			WHERE	TABL_CODE_SOSOK	= '"+sales.SOSOK+"'\
				AND TABL_CODE_UJCD	= '"+sales.Ujcd+"'\
				AND TABL_CODE_USE	= '1' \
			GROUP BY CHEK_TRAN_ACNO,		TABL_CODE_NAME, \
				TABL_CODE_TBNO, \
				CASH_BILL_BILL,		TABL_CODE_SORT  \
			ORDER BY TABL_CODE_SORT,	TABL_CODE_TBNO,		CASH_BILL_BILL	";

	result R( T.exec(SQL) );

	int row = 0; 
	std::string chek_tran_acno[R.size()];
	std::string tabl_code_name[R.size()];
	std::string tabl_code_tbno[R.size()];
	std::string cash_bill_bill[R.size()];
	int cash_sum[R.size()];
    for (result::const_iterator c = R.begin(); c != R.end(); ++c)
    {	
		chek_tran_acno[row] = c["CHEK_TRAN_ACNO"].as(string());
      	tabl_code_name[row] = c["TABL_CODE_NAME"].as(string());					
      	tabl_code_tbno[row] = c["TABL_CODE_TBNO"].as(string());
		cash_bill_bill[row] = c["CASH_BILL_BILL"].as(string());
		cash_sum[row] = c[5].as(int());
      	row++;
    }
    T.commit();
	int count = 0;

  	for(int j = 0; j < 8; j++)	// 세로 8개
  	{
    	 for(int i = 0; i < 5; i++)	// 가로 8개
     	{ 
        	if(count < R.size())	// DB 설정된 테이블 갯수 
        	{
				std::string tablelabel;
				if(chek_tran_acno[count].compare("") != 0)
				{
					tablelabel = tabl_code_name[count] +"\n"+ sep_thousands(cash_sum[count]);
					m_Button_Table[i][j] = Gtk::manage(new Gtk::Button(tablelabel));
					m_Button_Table[i][j]->modify_bg(Gtk::STATE_ACTIVE, color2);
					m_Button_Table[i][j]->modify_bg(Gtk::STATE_NORMAL, color2);
					m_Button_Table[i][j]->set_size_request(118, 70);
					m_Button_Table[i][j]->show();
					m_Table->attach(*m_Button_Table[i][j], i, i + 1, j, j + 1);
				}
				else
				{
					tablelabel = tabl_code_name[count];
					m_Button_Table[i][j] = Gtk::manage(new Gtk::Button(tablelabel));
					m_Button_Table[i][j]->modify_bg(Gtk::STATE_ACTIVE, color);
					m_Button_Table[i][j]->modify_bg(Gtk::STATE_NORMAL, color);
					m_Button_Table[i][j]->set_size_request(118, 70);
					m_Button_Table[i][j]->show();
					m_Table->attach(*m_Button_Table[i][j], i, i + 1, j, j + 1);
				}
	    		// 시그널 이벤트 설정부분 (라벨값 전달)
				// 체크인된 테이블 확인.
				if(chek_tran_acno[count].compare("") == 0)
					m_Button_Table[i][j]->signal_clicked().connect( sigc::bind<std::string>( sigc::mem_fun(*this, &MainWindow::on_Button_Table_clicked ), tabl_code_tbno[count]) );
				else {
				// 이벤트 함수로 손님번호 & 빌번호 & 테이블번호 전달
					std::string acno_bill = chek_tran_acno[count] +"&"+ cash_bill_bill[count] +"&"+ tabl_code_tbno[count]; 
					m_Button_Table[i][j]->signal_clicked().connect( sigc::bind<std::string>( sigc::mem_fun(*this, &MainWindow::on_Button_Chekin_Table_clicked ), acno_bill) );
				}
	      	}
	      	else
	      	{
	      		m_Button_Table[i][j] = Gtk::manage(new Gtk::Button(""));
	      		m_Button_Table[i][j]->set_size_request(118, 70);
	      		m_Button_Table[i][j]->show();
	      		m_Table->attach(*m_Button_Table[i][j], i, i + 1, j, j + 1);
	      	}
			count++;
     	}
  	}
  	table_low_show();
  	windowstatus = "table";
}
// 테이블 관련 하단 버튼 생성
void MainWindow::table_low_show()
{	
	Gdk::Color color;
	color.set("#008800");
	initlowbutton();
	// 하단 버튼 네임 설정 변수
	string low_button_name[2][5] = {"테이블이체", "테이블변경", "테이블합석", "영수증합산", "영수증분리", \
									"빌찾기", "돈통열기", "마감", "보고서", "체크아웃" };
  	for(int j = 0; j < 2; j++)	// 세로 2개
  	{
    	 for(int i = 0; i < 5; i++)	// 가로 5개
     	{     		
   			m_Button_Table_Low[i][j] = Gtk::manage(new Gtk::Button(low_button_name[j][i]));
   			m_Button_Table_Low[i][j]->set_size_request(30, 30);
			m_Button_Table_Low[i][j]->modify_bg(Gtk::STATE_ACTIVE, color);
			m_Button_Table_Low[i][j]->modify_bg(Gtk::STATE_NORMAL, color);
   			m_Button_Table_Low[i][j]->show();
  			m_Table_Low[j]->attach(*m_Button_Table_Low[i][j], i, i+1, j, j+1);
	    
   			// 시그널 이벤트 설정부분 (라벨값 전달)
   			m_Button_Table_Low[i][j]->signal_clicked().connect( sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &MainWindow::on_Button_Table_Low_clicked ), m_Button_Table_Low[i][j]->get_label() ) );
   		}
   	}
}
//******************************************************************************
//		테이블 화면 관련 시그널 이벤트 함수
//******************************************************************************
// 빈 테이블 버튼 클릭 시
void MainWindow::on_Button_Table_clicked(std::string data)
{
	if(data.compare("테이블") == 0)
	{
		table_show(1);
	}
	else {
		if(tablestatus.compare("change") == 0)
		{
			cout << "테이블" << data << "이동" << endl;
			table_change(data);
			tablestatus = "normal";
		}
		// 새로운 테이블
		else {
			acctnum();
			billnum();
			sales.nowACCT = sales.maxACCT;
			sales.nowBILL = sales.maxBILL;
			cout << sales.maxACCT << sales.maxBILL << endl;
			m_refListStore->clear();
			m_refListStore_Detail->clear();
			for(int i=0; i<12; i++)
			{
				Gtk::TreeRow treerow = *(m_refListStore_Detail->append());
				treerow[m_detail_columns.text]     = sales.detailstr[i];
				switch(i) {
					case 0:
						treerow[m_detail_columns.contents] = sales.nowACCT;
						break;
					case 1:
						treerow[m_detail_columns.contents] = data;
						break;
					case 2:
						treerow[m_detail_columns.contents] = sales.nowBILL;
						break;
					case 3:
						treerow[m_detail_columns.contents] = "1";
						break;
					case 4:
						treerow[m_detail_columns.contents] = sales.Workdate +" - "+ sales.Time;
						break;
					case 8:
						treerow[m_detail_columns.contents] = sales.User;
						break;
					case 9:
						treerow[m_detail_columns.contents] = sales.Workdate +" - "+ sales.Time;
						break;
					default:
						treerow[m_detail_columns.contents] = " ";
						break;
				};
			}

			sales.nowtablnum = data;
			cout << data << "\n" ;
			menu_show("1");
		}
		// 기존 테이블
	}
}
// 체크인된 테이블 선택시
void MainWindow::on_Button_Chekin_Table_clicked(std::string data)
{	
	if(tablestatus.compare("transfer") == 0)
	{
		vector<string> tokens;
		string delimiters = "&";
		string::size_type lastPos = data.find_first_not_of(delimiters, 0);
		string::size_type pos     = data.find_first_of(delimiters, lastPos);
		while (string::npos != pos || string::npos != lastPos)
		{
			tokens.push_back(data.substr(lastPos, pos - lastPos));
			lastPos = data.find_first_not_of(delimiters, pos);
			pos = data.find_first_of(delimiters, lastPos);
		}	/* tokens[0] = ACCT , token[1] = BILL , token[2] = table number */
		sales.transACCT = tokens[0];
		sales.transBILL = tokens[1];
		sales.transtablnum = tokens[2];
		if(sales.nowtablnum.compare(sales.transtablnum) == 0 && sales.nowACCT.compare(sales.transACCT) == 0) {
			msgwindow_show("같은 테이블로 이체 할 수 없습니다.");
		}
		else tabletranswindow->show();
		tablestatus = "normal";
	}
	else if(tablestatus.compare("billadd") == 0)
	{
		// 대상 테이블의 빌번호로 모든 메뉴를 변경
		vector<string> tokens;
		string delimiters = "&";
		string::size_type lastPos = data.find_first_not_of(delimiters, 0);
		string::size_type pos     = data.find_first_of(delimiters, lastPos);
		while (string::npos != pos || string::npos != lastPos)
		{
			tokens.push_back(data.substr(lastPos, pos - lastPos));
			lastPos = data.find_first_not_of(delimiters, pos);
			pos = data.find_first_of(delimiters, lastPos);
		}	/* tokens[0] = ACCT , token[1] = BILL , token[2] = table number */
		sales.transACCT = tokens[0];
		sales.transBILL = tokens[1];
		sales.transtablnum = tokens[2];
		if(sales.nowtablnum.compare(sales.transtablnum) == 0 && sales.nowACCT.compare(sales.transACCT) == 0) {
			msgwindow_show("같은 테이블로 합산 할 수 없습니다.");
		}
		else {
			work T(C, "billadd");
			SQL=  " SELECT \
						CASH_TRAN_SEQ \
					FROM P_CASH_TRAN \
					WHERE \
						CASH_TRAN_SOSOK	='"+sales.SOSOK+"' \
						AND CASH_TRAN_UJCD	='"+sales.Ujcd+"' \
						AND CASH_TRAN_DATE	='"+sales.Workdate+"' \
						AND CASH_TRAN_ACNO	='"+sales.nowACCT+"' \
						AND CASH_TRAN_BILL	='"+sales.nowBILL+"' \
						AND CASH_TRAN_VOID_STAU	='' \
						AND CASH_TRAN_TRAN_STAU	=''";
			result R( T.exec(SQL) );
			T.commit();
			int count = 0;
			for (result::const_iterator c = R.begin(); c != R.end(); ++c)
			{
				CashTransfer(sales.nowACCT,c[0].as(string()),sales.transACCT,sales.transBILL);
			}
			table_show(0);
			tablestatus = "normal";
		}
	}
	else {
		vector<string> tokens;
		string delimiters = "&";
		string::size_type lastPos = data.find_first_not_of(delimiters, 0);
		string::size_type pos     = data.find_first_of(delimiters, lastPos);
		while (string::npos != pos || string::npos != lastPos)
		{
			tokens.push_back(data.substr(lastPos, pos - lastPos));
			lastPos = data.find_first_not_of(delimiters, pos);
			pos = data.find_first_of(delimiters, lastPos);
		}	/* tokens[0] = ACCT , token[1] = BILL , token[2] = table number */
		sales.nowACCT = tokens[0];
		sales.nowBILL = tokens[1];
		sales.nowtablnum = tokens[2];
		// Treeview 정보 삭제
		m_refListStore->clear();
		// 테이블 메뉴정보 퀴리문
		work T(C, "tablemenushow");
		SQL = " SELECT \
					CASH_TRAN_ACNO, CASH_TRAN_SEQ, CASH_TRAN_BILL, CASH_TRAN_TBNO, CASH_TRAN_MENU, \
					CASH_TRAN_DETA,	CASH_TRAN_CARD,	CASH_TRAN_CALC, CASH_TRAN_SDIV,	CASH_TRAN_VDIV, \
					CASH_TRAN_ADIV,	CASH_TRAN_BASE_AMT, CASH_TRAN_CNT, CASH_TRAN_DC_PER, CASH_TRAN_DC_AMT, \
					CASH_TRAN_AMT, CASH_TRAN_NET, CASH_TRAN_SVC, CASH_TRAN_VAT, CASH_TRAN_ADV, \
					CASH_TRAN_TOT, CASH_TRAN_GTOT, CASH_TRAN_CHARGE, CASH_TRAN_CREDIT, CASH_TRAN_VOID_STAU, \
					CASH_TRAN_VOID_SEQ, CASH_TRAN_VOID_MEMO, CASH_TRAN_TRAN_STAU, CASH_TRAN_TRAN_ACNO1, CASH_TRAN_TRAN_SEQ1, \
					CASH_TRAN_TRAN_ACNO2, CASH_TRAN_TRAN_SEQ2, CASH_TRAN_JUBDAE1, CASH_TRAN_JUBDAE2, MENU_CODE_DC_LOCK \
				FROM P_CASH_TRAN \
				LEFT JOIN P_MENU_CODE \
					ON	CASH_TRAN_SOSOK='"+sales.SOSOK+"'\
					AND CASH_TRAN_UJCD='"+sales.Ujcd+"'\
					AND CASH_TRAN_DATE='"+sales.Workdate+"' \
					AND CASH_TRAN_ACNO='"+tokens[0]+"'\
					AND CASH_TRAN_BILL='"+tokens[1]+"'\
					AND CASH_TRAN_SOSOK=MENU_CODE_SOSOK \
					AND CASH_TRAN_MENU=MENU_CODE_CODE \
				WHERE   CASH_TRAN_SOSOK='"+sales.SOSOK+"'\
					AND CASH_TRAN_UJCD='"+sales.Ujcd+"'\
					AND CASH_TRAN_DATE='"+sales.Workdate+"' \
					AND CASH_TRAN_ACNO='"+tokens[0]+"'\
					AND CASH_TRAN_BILL='"+tokens[1]+"'\
				ORDER BY CASH_TRAN_SEQ ASC"; 
		result R( T.exec(SQL) );
		
		// DB값 트리뷰에 저장
		int row = 0; 
		for (result::const_iterator c = R.begin(); c != R.end(); ++c)
		{			
			CellItem_Product foo;
			foo.m_code = c["CASH_TRAN_MENU"].as(int());
			foo.m_number = c["CASH_TRAN_CNT"].as(int());
			foo.m_product = c["CASH_TRAN_DETA"].as(string());
			if(c["CASH_TRAN_MENU"].as(int()) == 0) {
				foo.m_price = sep_thousands(c["CASH_TRAN_CREDIT"].as(int()));
			}
			else {
				foo.m_price = sep_thousands(c["CASH_TRAN_AMT"].as(int()) * c["CASH_TRAN_CNT"].as(int()));
			}
			foo.m_div = 0;		
			foo.m_svc = c["CASH_TRAN_SVC"].as(int()) * c["CASH_TRAN_CNT"].as(int());
			foo.m_vat = c["CASH_TRAN_VAT"].as(int()) * c["CASH_TRAN_CNT"].as(int());
			foo.m_adv = c["CASH_TRAN_ADV"].as(int()) * c["CASH_TRAN_CNT"].as(int());
			foo.m_rate = c["CASH_TRAN_AMT"].as(int());
			foo.m_net = c["CASH_TRAN_NET"].as(int()) * c["CASH_TRAN_CNT"].as(int());
			foo.m_tot = c["CASH_TRAN_TOT"].as(int());
			foo.m_dc = c["CASH_TRAN_DC_AMT"].as(int());
			foo.m_credit = c["CASH_TRAN_CREDIT"].as(int());
			liststore_add_item(foo);
			
			row++;
		}

		// 트리뷰 하단 쿼리문
		SQL = " SELECT \
					CHEK_TRAN_ACNO, TABL.TABL_CODE_NAME AS TABLE_NAME, \
					CASH_BILL_BILL,	CHEK_TRAN_PERS, CHEK_TRAN_FDATE, CHEK_TRAN_STAU, \
					CHEK_TRAN_MEMO,	USER2.OPER_USER_NAME AS BUSER_NAME,	\
					USER3.OPER_USER_NAME AS FUSER_NAME, \
					CHEK_TRAN_FDATE, USER4.OPER_USER_NAME AS LUSER_NAME, \
					CHEK_TRAN_LDATE, CHEK_TRAN_YUSER, CHEK_TRAN_BUSER, CHEK_TRAN_TBNO \
				FROM P_CHEK_TRAN \
				LEFT JOIN P_CASH_BILL \
					ON  CHEK_TRAN_SOSOK		= CASH_BILL_SOSOK \
					AND CHEK_TRAN_UJCD		= CASH_BILL_UJCD \
					AND CHEK_TRAN_DATE		= CASH_BILL_DATE \
					AND CHEK_TRAN_ACNO		= CASH_BILL_ACNO \
				LEFT JOIN TB_OPER_USER USER2 \
					ON  CHEK_TRAN_BUSER		= USER2.OPER_USER_ID \
				LEFT JOIN TB_OPER_USER USER3 \
					ON  CHEK_TRAN_FUSER		= USER3.OPER_USER_ID \
				LEFT JOIN TB_OPER_USER USER4 \
					ON  CHEK_TRAN_LUSER		= USER4.OPER_USER_ID \
				LEFT JOIN P_TABL_CODE TABL \
					ON  CHEK_TRAN_SOSOK		= TABL.TABL_CODE_SOSOK \
					AND CHEK_TRAN_UJCD		= TABL.TABL_CODE_UJCD \
					AND CHEK_TRAN_TBNO		= TABL.TABL_CODE_TBNO \
				WHERE \
					CHEK_TRAN_SOSOK='"+sales.SOSOK+"'\
					AND CHEK_TRAN_UJCD='"+sales.Ujcd+"'\
					AND CHEK_TRAN_DATE='"+sales.Workdate+"'\
					AND CHEK_TRAN_ACNO='"+tokens[0]+"'";
		result R2( T.exec(SQL) );
		// DB값 트리뷰에 저장
		m_refListStore_Detail->clear();
		result::const_iterator c = R2.begin();

		for(int i=0; i<12; i++)
		{
			Gtk::TreeRow treerow = *(m_refListStore_Detail->append());
			treerow[m_detail_columns.text]     = sales.detailstr[i];
			treerow[m_detail_columns.contents] = c[i].as(string());
		}
		T.commit();
		price_info();
	}
}
// 테이블 하단버튼 클릭
void MainWindow::on_Button_Table_Low_clicked(Glib::ustring data)
{
	// 1번째 줄
	if(data.compare("테이블이체") == 0)
	{
		//선택된 테이블이 없다면.
		if(sales.nowACCT.compare("0") == 0) {
			msgwindow_show("선택된 테이블이 없습니다.      ");
			tablestatus = "normal";
		}
		else {
			tablestatus = "transfer";
		}
	}
	else if(data.compare("테이블변경") == 0)
	{
		//선택된 테이블이 없다면.
		if(sales.nowACCT.compare("0") == 0) {
			msgwindow_show("선택된 테이블이 없습니다.      ");
			tablestatus = "normal";
		}
		else { tablestatus = "change"; }
	}
	else if(data.compare("테이블합석") == 0)
	{
		//선택된 테이블이 없다면.
		if(sales.nowACCT.compare("0") == 0) {
			msgwindow_show("선택된 테이블이 없습니다.      ");
			tablestatus = "normal";
		}
		else { tablestatus = "tableadd";
			acctnum();
			billnum();
			sales.nowACCT = sales.maxACCT;
			sales.nowBILL = sales.maxBILL;
			cout << sales.maxACCT << sales.maxBILL << endl;
			m_refListStore->clear();
			m_refListStore_Detail->clear();
			for(int i=0; i<12; i++)
			{
				Gtk::TreeRow treerow = *(m_refListStore_Detail->append());
				treerow[m_detail_columns.text]     = sales.detailstr[i];
				switch(i) {
					case 0:
						treerow[m_detail_columns.contents] = sales.nowACCT;
						break;
					case 1:
						treerow[m_detail_columns.contents] = sales.nowtablnum;
						break;
					case 2:
						treerow[m_detail_columns.contents] = sales.nowBILL;
						break;
					case 3:
						treerow[m_detail_columns.contents] = "1";
						break;
					case 4:
						treerow[m_detail_columns.contents] = sales.Workdate +" - "+ sales.Time;
						break;
					case 8:
						treerow[m_detail_columns.contents] = sales.User;
						break;
					case 9:
						treerow[m_detail_columns.contents] = sales.Workdate +" - "+ sales.Time;
						break;
					default:
						treerow[m_detail_columns.contents] = " ";
						break;
				};
			}
			menu_show("1");
		}
	}
	else if(data.compare("영수증합산") == 0)
	{
		if(sales.nowACCT.compare("0") == 0) {
			msgwindow_show("선택된 테이블이 없습니다.      ");
			tablestatus = "normal";
		}
		else { tablestatus = "billadd"; }
	}
	else if(data.compare("영수증분리") == 0)
	{
		if(sales.nowACCT.compare("0") == 0) {
			msgwindow_show("선택된 테이블이 없습니다.      ");
			tablestatus = "normal";
		}
		else { 
			tablestatus = "billtrans";
			tabletranswindow->show();
		}
	}
	// 두번째줄
	else if(data.compare("빌찾기") == 0)
	{
		billfindwindow->show();
	}
	else if(data.compare("보고서") == 0)
	{
		reportwindow_show();
	}
	else if(data.compare("돈통열기") == 0)
	{
		CashDrawer.OpenDrawer();
	}
	else if(data.compare("마감") == 0)
	{
		m_WorkClose_Entry_Workdate = sales.Workdate;
		m_WorkClose_Entry_User = sales.User;
		m_WorkClose_Entry_Ujname = sales.Ujname;
		m_pMainWindowMap->transfer_variables_to_widgets();
		workclosewindow->show();
	}
	else if(data.compare("체크아웃") == 0)
	{
		if( atoi(m_Label_Price_info[7]->get_label().data()) > 0) {
			msgwindow_show("발란스가 맞지 않아\n체크아웃 할수 없습니다.");
		}
		else { 
			chekout(); 
			treeview_init();
			table_show(1);
		}
	}
}
//******************************************************************************
//		테이블 변경 관련 함수
//******************************************************************************
void MainWindow::table_change(Glib::ustring data)
{
	work T(C, "tablechange");
	SQL = " UPDATE P_CHEK_TRAN SET \
				CHEK_TRAN_TBNO = '" +data+ "', \
				CHEK_TRAN_LDATE = NOW(), \
				CHEK_TRAN_LUSER = '" +sales.User+ "' \
			WHERE \
					CHEK_TRAN_SOSOK='"+sales.SOSOK+"'\
				AND CHEK_TRAN_UJCD='"+sales.Ujcd+"'\
				AND CHEK_TRAN_DATE='"+sales.Workdate+"' \
				AND CHEK_TRAN_ACNO='"+sales.nowACCT+"'\
				AND CHEK_TRAN_STAU='"+sales.CHKINCODE+"'";
	result R( T.exec(SQL) );
	T.commit();
	table_show(0);
}
void MainWindow::CashTransfer(string nowACCT, string SEQ, string transACCT, string transBILL)
{
	std::string nowSEQ, transSEQ;
	work T(C, "billaddloop");
	SQL = " SELECT \
				MAX(CASH_TRAN_SEQ) + 1 \
				FROM P_CASH_TRAN \
			WHERE \
				CASH_TRAN_SOSOK	='"+sales.SOSOK+"' \
			AND CASH_TRAN_UJCD	='"+sales.Ujcd+"' \
			AND CASH_TRAN_DATE	='"+sales.Workdate+"' \
			AND CASH_TRAN_ACNO	='"+sales.nowACCT+"'";
	result R( T.exec(SQL) );
	result::const_iterator c = R.begin();
	nowSEQ = c[0].as(string());
	
	SQL = " SELECT \
				MAX(CASH_TRAN_SEQ) + 1 \
				FROM P_CASH_TRAN \
			WHERE \
				CASH_TRAN_SOSOK	='"+sales.SOSOK+"' \
			AND CASH_TRAN_UJCD	='"+sales.Ujcd+"' \
			AND CASH_TRAN_DATE	='"+sales.Workdate+"' \
			AND CASH_TRAN_ACNO	='"+sales.transACCT+"'";
	result R1( T.exec(SQL) );
	result::const_iterator c1 = R1.begin();
	transSEQ = c1[0].as(string());

	SQL=  " UPDATE P_CASH_TRAN SET \
				CASH_TRAN_TRAN_STAU='TR1', \
				CASH_TRAN_TRAN_ACNO2='"+transACCT+"', \
				CASH_TRAN_TRAN_SEQ2='"+transSEQ+"', \
				CASH_TRAN_LDATE=NOW(), \
				CASH_TRAN_LUSER='"+sales.User+"' \
			WHERE \
				CASH_TRAN_SOSOK	='"+sales.SOSOK+"' \
				AND CASH_TRAN_UJCD	='"+sales.Ujcd+"' \
				AND CASH_TRAN_DATE	='"+sales.Workdate+"' \
				AND CASH_TRAN_ACNO	='"+nowACCT+"' \
				AND CASH_TRAN_SEQ	='"+SEQ+"'";
	result R2( T.exec(SQL) );

	// 이체할 테이블 - 로 표기	
	SQL = " INSERT INTO P_CASH_TRAN \
				(CASH_TRAN_SOSOK, CASH_TRAN_UJCD,   CASH_TRAN_DATE, \
				 CASH_TRAN_ACNO,  CASH_TRAN_SEQ,    CASH_TRAN_BILL, \
				 CASH_TRAN_TBNO,  CASH_TRAN_MENU,   CASH_TRAN_DETA, \
				 CASH_TRAN_CARD,  CASH_TRAN_CALC,   CASH_TRAN_SDIV, \
				 CASH_TRAN_VDIV,  CASH_TRAN_ADIV,   CASH_TRAN_BASE_AMT, \
				 CASH_TRAN_CNT,   CASH_TRAN_DC_PER,	CASH_TRAN_DC_AMT, \
				 CASH_TRAN_AMT,	  CASH_TRAN_NET,	CASH_TRAN_SVC, \
				 CASH_TRAN_VAT,	  CASH_TRAN_ADV,	CASH_TRAN_TOT, \
				 CASH_TRAN_GTOT,  CASH_TRAN_CHARGE,	CASH_TRAN_CREDIT, \
				 CASH_TRAN_VOID_STAU,  CASH_TRAN_VOID_SEQ,   CASH_TRAN_VOID_MEMO, \
				 CASH_TRAN_TRAN_STAU,  CASH_TRAN_TRAN_ACNO1, CASH_TRAN_TRAN_SEQ1, \
				 CASH_TRAN_TRAN_ACNO2, CASH_TRAN_TRAN_SEQ2,  CASH_TRAN_JUBDAE1, \
				 CASH_TRAN_JUBDAE2,	   CASH_TRAN_FDATE,		 CASH_TRAN_FUSER, \
				 CASH_TRAN_LDATE,	   CASH_TRAN_LUSER) \
			SELECT \
				 CASH_TRAN_SOSOK,	 	CASH_TRAN_UJCD, 	  CASH_TRAN_DATE, \
				 CASH_TRAN_ACNO, 		'"+nowSEQ+"', 		  CASH_TRAN_BILL, \
				 CASH_TRAN_TBNO, 		CASH_TRAN_MENU,		  CASH_TRAN_DETA, \
				 CASH_TRAN_CARD, 		CASH_TRAN_CALC,		  CASH_TRAN_SDIV, \
				 CASH_TRAN_VDIV, 		CASH_TRAN_ADIV, 	  CASH_TRAN_BASE_AMT, \
				 CASH_TRAN_CNT*-1, 		CASH_TRAN_DC_PER,     CASH_TRAN_DC_AMT, \
				 CASH_TRAN_AMT, 		CASH_TRAN_NET,	  	  CASH_TRAN_SVC, \
				 CASH_TRAN_VAT, 		CASH_TRAN_ADV, 		  CASH_TRAN_TOT, \
				 CASH_TRAN_GTOT*-1, 	CASH_TRAN_CHARGE*-1,  CASH_TRAN_CREDIT, \
				 CASH_TRAN_VOID_STAU,   CASH_TRAN_VOID_SEQ,   CASH_TRAN_VOID_MEMO, \
				 'TR2', 				CASH_TRAN_TRAN_ACNO1, CASH_TRAN_TRAN_SEQ1, \
				 '"+transACCT+"', 		'"+transSEQ+"', 	  CASH_TRAN_JUBDAE1, \
				 CASH_TRAN_JUBDAE2, 	CASH_TRAN_FDATE, 	  CASH_TRAN_FUSER, \
				 NOW(), '"+sales.User+"' \
			FROM P_CASH_TRAN \
			WHERE \
				CASH_TRAN_SOSOK	='"+sales.SOSOK+"' \
				AND CASH_TRAN_UJCD	='"+sales.Ujcd+"' \
				AND CASH_TRAN_DATE	='"+sales.Workdate+"' \
				AND CASH_TRAN_ACNO	='"+nowACCT+"' \
				AND CASH_TRAN_SEQ	='"+SEQ+"'";
	result R3( T.exec(SQL) );	

	SQL = " INSERT INTO P_CASH_TRAN \
				(CASH_TRAN_SOSOK, CASH_TRAN_UJCD,   CASH_TRAN_DATE, \
				 CASH_TRAN_ACNO,  CASH_TRAN_SEQ,    CASH_TRAN_BILL, \
				 CASH_TRAN_TBNO,  CASH_TRAN_MENU,   CASH_TRAN_DETA, \
				 CASH_TRAN_CARD,  CASH_TRAN_CALC,   CASH_TRAN_SDIV, \
				 CASH_TRAN_VDIV,  CASH_TRAN_ADIV,   CASH_TRAN_BASE_AMT, \
				 CASH_TRAN_CNT,   CASH_TRAN_DC_PER,	CASH_TRAN_DC_AMT, \
				 CASH_TRAN_AMT,	  CASH_TRAN_NET,	CASH_TRAN_SVC, \
				 CASH_TRAN_VAT,	  CASH_TRAN_ADV,	CASH_TRAN_TOT, \
				 CASH_TRAN_GTOT,  CASH_TRAN_CHARGE,	CASH_TRAN_CREDIT, \
				 CASH_TRAN_VOID_STAU,  CASH_TRAN_VOID_SEQ,   CASH_TRAN_VOID_MEMO, \
				 CASH_TRAN_TRAN_STAU,  CASH_TRAN_TRAN_ACNO1, CASH_TRAN_TRAN_SEQ1, \
				 CASH_TRAN_TRAN_ACNO2, CASH_TRAN_TRAN_SEQ2,  CASH_TRAN_JUBDAE1, \
				 CASH_TRAN_JUBDAE2,	   CASH_TRAN_FDATE,		 CASH_TRAN_FUSER, \
				 CASH_TRAN_LDATE,	   CASH_TRAN_LUSER) \
			SELECT \
				 CASH_TRAN_SOSOK,      CASH_TRAN_UJCD,       CASH_TRAN_DATE, \
				 '"+transACCT+"',      '"+transSEQ+"',	     '"+transBILL+"', \
				 CASH_TRAN_TBNO,       CASH_TRAN_MENU,   	 CASH_TRAN_DETA, \
				 CASH_TRAN_CARD, 	   CASH_TRAN_CALC,   	 CASH_TRAN_SDIV, \
				 CASH_TRAN_VDIV,  	   CASH_TRAN_ADIV,   	 CASH_TRAN_BASE_AMT, \
				 CASH_TRAN_CNT,        CASH_TRAN_DC_PER,	 CASH_TRAN_DC_AMT, \
				 CASH_TRAN_AMT,	  	   CASH_TRAN_NET,		 CASH_TRAN_SVC, \
				 CASH_TRAN_VAT,	  	   CASH_TRAN_ADV,		 CASH_TRAN_TOT, \
				 CASH_TRAN_GTOT,       CASH_TRAN_CHARGE,	 CASH_TRAN_CREDIT, \
				 CASH_TRAN_VOID_STAU,  CASH_TRAN_VOID_SEQ,   CASH_TRAN_VOID_MEMO, \
				 '',  			   	   CASH_TRAN_TRAN_ACNO1, CASH_TRAN_TRAN_SEQ1, \
				 '0',		   		   '0', 		 		 CASH_TRAN_JUBDAE1, \
				 CASH_TRAN_JUBDAE2,	   CASH_TRAN_FDATE,		 CASH_TRAN_FUSER, \
				 NOW(),	  			   '"+sales.User+"' \
			FROM P_CASH_TRAN \
			WHERE \
				CASH_TRAN_SOSOK	='"+sales.SOSOK+"' \
				AND CASH_TRAN_UJCD	='"+sales.Ujcd+"' \
				AND CASH_TRAN_DATE	='"+sales.Workdate+"' \
				AND CASH_TRAN_ACNO	='"+nowACCT+"' \
				AND CASH_TRAN_SEQ	='"+SEQ+"'";
	result R4( T.exec(SQL) );

	T.commit();
}

//******************************************************************************
//
//		메뉴 화면 관련 함수
//
//*****************************************************************************
void MainWindow::menu_show(std::string menu_grup_code)
{
	if( windowstatus.compare("menu") != 0 || menu_grup_code.compare(check_menu_grp) !=0)
	{
		Gdk::Color color;
		color.set("#CC3366");
		check_menu_grp = menu_grup_code;
		initbutton();
		
		work T(C, "menu");
		SQL = " SELECT \
					B.MENU_CODE_CODE, \
					B.MENU_CODE_HNAM, \
					B.MENU_CODE_RATE \
				FROM P_MENU_GRUP A \
				LEFT JOIN P_MENU_CODE B \
					ON A.MENU_GRUP_SOSOK 	= '"+sales.SOSOK+"'\
					AND A.MENU_GRUP_UJCD	= '"+sales.Ujcd+"'\
					AND A.MENU_GRUP_CODE	= '"+menu_grup_code+"'\
					AND A.MENU_GRUP_SOSOK	= B.MENU_CODE_SOSOK \
					AND A.MENU_GRUP_MENU	= B.MENU_CODE_CODE \
				WHERE  \
					A.MENU_GRUP_SOSOK		= '"+sales.SOSOK+"'\
					AND A.MENU_GRUP_UJCD	= '"+sales.Ujcd+"'\
					AND A.MENU_GRUP_CODE	= '"+menu_grup_code+"'\
				ORDER BY A.MENU_GRUP_POSI ASC";
		result R( T.exec(SQL) );
		int row = 0; 
		std::string menu_code_code[R.size()];
		std::string menu_code_hnam[R.size()];
		int menu_code_rate[R.size()];
    	for (result::const_iterator c = R.begin(); c != R.end(); ++c)
    	{
    	  	menu_code_code[row] = c["MENU_CODE_CODE"].as(string());
    	  	menu_code_hnam[row] = c["MENU_CODE_HNAM"].as(string());
    	  	menu_code_rate[row] = c["MENU_CODE_RATE"].as(int());
    	  	row++;
    	}
    	T.commit();
		int count = 0;  // 메뉴 카운트
  		for(int j = 0; j < 8; j++)	// 세로 8개
  		{
   		 	 for(int i = 0; i < 5; i++)	// 가로 8개
   		  	{
     	 	 	// DB 연결 테이블 라벨 설정 부분
       		 	if(count < R.size())	// DB 설정된 테이블 
      	 	 	{
       		 		std::string menulabel = menu_code_hnam[count] +"\n"+sep_thousands(menu_code_rate[count]);
        			m_Button_Menu[i][j] = Gtk::manage(new Gtk::Button(menulabel));        	
					m_Button_Menu[i][j]->modify_bg(Gtk::STATE_ACTIVE, color);
					m_Button_Menu[i][j]->modify_bg(Gtk::STATE_NORMAL, color);
        			m_Button_Menu[i][j]->set_size_request(118, 70);
        			m_Button_Menu[i][j]->show();
	    			m_Table->attach(*m_Button_Menu[i][j], i, i + 1, j, j + 1);
	    	
	    			// 시그널 이벤트 설정부분 (라벨값 전달)
	      			m_Button_Menu[i][j]->signal_clicked().connect( sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &MainWindow::on_Button_Menu_clicked ), menu_code_code[count]) );
	      		}
	      		else
	      		{
	      			m_Button_Menu[i][j] = Gtk::manage(new Gtk::Button(""));
	      			m_Button_Menu[i][j]->set_size_request(118, 70);
	      			m_Button_Menu[i][j]->show();
	      			m_Table->attach(*m_Button_Menu[i][j], i, i + 1, j, j + 1);
	      		}
	      		count++;
     		}
  		} 
  		menu_low_show();  		
	}
  	windowstatus = "menu";
}
// 메뉴관련 하단 버튼 생성
void MainWindow::menu_low_show()
{
	initlowbutton();
	Gdk::Color color;
	color.set("#CC0066");
	// 버튼이름 초기화 상단은 DB값
	string low_button_name[2][5] = {"", "", "", "", "", \
									"현금입금", "수량", "주문완료", "취소", "메뉴찾기" };
	// 메뉴 그룹 얻기
	work T(C, "menugrup");
	SQL = " SELECT \
				MENU_GRUP_CODE, \
				MENU_GRUP_NAME \
			FROM \
			P_MENU_GRUP_CODE \
			WHERE \
				MENU_GRUP_SOSOK = '"+sales.SOSOK+"'\
			AND \
				MENU_GRUP_UJCD = '"+sales.Ujcd+"'\
			ORDER BY MENU_GRUP_SORT ASC";
	result R( T.exec(SQL) );
	int row = 0; 
	// 변수 선언
	std::string menu_grup_code[R.size()];
	std::string menu_grup_name[R.size()];
	// 값 저장
   	for (result::const_iterator c = R.begin(); c != R.end(); ++c)
   	{
   	  	menu_grup_code[row] = c["MENU_GRUP_CODE"].as(string());
   	  	menu_grup_name[row] = c["MENU_GRUP_NAME"].as(string());
   	  	low_button_name[0][row] = menu_grup_name[row];
   	  	row++;
   	}
   	T.commit();

	int count = 0;
  	for(int j = 0; j < 2; j++)	// 세로 2개
  	{	
    	 for(int i = 0; i < 5; i++)	// 가로 5개
     	{ 
     		m_Button_Menu_Low[i][j] = Gtk::manage(new Gtk::Button(low_button_name[j][i]));
   			m_Button_Menu_Low[i][j]->set_size_request(30, 30);
			m_Button_Menu_Low[i][j]->modify_bg(Gtk::STATE_ACTIVE, color);
			m_Button_Menu_Low[i][j]->modify_bg(Gtk::STATE_NORMAL, color);
   			m_Button_Menu_Low[i][j]->show();
  			m_Table_Low[j]->attach(*m_Button_Menu_Low[i][j], i, i+1, j, j+1);
  			// 메뉴 그룹버튼 시그널
  			if(j == 0 && count < R.size()) {
   				m_Button_Menu_Low[i][j]->signal_clicked().connect( sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &MainWindow::on_Button_Menu_Low_clicked ), menu_grup_code[count]) );
   				count++;
   			}
   			// 기타 버튼 시그널
   			else if(j == 1) {
   				m_Button_Menu_Low[i][j]->signal_clicked().connect( sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &MainWindow::on_Button_Menu_Low_clicked ), low_button_name[j][i]) );
   			}
		}	   
	}
}
//******************************************************************************
//		메뉴 화면 관련 시그널 이벤트 함수
//******************************************************************************
// 메뉴 버튼 클릭
void MainWindow::on_Button_Menuadd_clicked(std::string data)
{
	if(sales.nowACCT.compare("-1") == 0 && sales.nowBILL.compare("-1"))
	{
		msgwindow_show("테이블을 선택하세요");
		
	}
	else
	{
		type_children children = m_refListStore->children();
		if(children.size() !=0)	/* 등록된 데이타가 있다면 추가주문*/
		{
			menustatus = "menuadd";
			menu_show("1");
		}
	}
}
void MainWindow::on_Button_Menu_clicked(Glib::ustring data)
{
	work T(C, "menucode");
	SQL = 
	"SELECT \
		MENU_CODE_HNAM, \
		MENU_CODE_TOT, \
		MENU_CODE_CODE, \
		MENU_CODE_DIV, \
		MENU_CODE_SVC, \
		MENU_CODE_VAT, \
		MENU_CODE_ADV, \
		MENU_CODE_RATE, \
		MENU_CODE_NET, \
		MENU_CODE_TOT \
	FROM \
		P_MENU_CODE \
	WHERE \
		MENU_CODE_SOSOK = '" +sales.SOSOK+ "' \
	AND \
		MENU_CODE_CODE = '" +data+ "'";
	result R( T.exec(SQL) );

	result::const_iterator c = R.begin();
 
	CellItem_Product foo;
	foo.m_number = 1;									/*수량*/
	foo.m_product = c["MENU_CODE_HNAM"].as(string());	/*이름*/
	foo.m_rate = c["MENU_CODE_RATE"].as(int());			/*가격*/
	foo.m_price = sep_thousands(c["MENU_CODE_RATE"].as(int()));		/*가격x수량*/
  	foo.m_code = c["MENU_CODE_CODE"].as(int());			/*고유번호*/
  	foo.m_div = c["MENU_CODE_DIV"].as(int());			/*세금*/
	if(c["MENU_CODE_SVC"].as(int()) == 1 && c["MENU_CODE_VAT"].as(int()) == 1) {	/*봉사료*/
		foo.m_svc = (int)(c["MENU_CODE_RATE"].as(int()) / 12.1);	
	}
	else if(c["MENU_CODE_SVC"].as(int()) == 1) { 
		foo.m_svc = c["MENU_CODE_RATE"].as(int()) / 10;			
	}
  	else { foo.m_svc = c["MENU_CODE_SVC"].as(int()); }				
	if(c["MENU_CODE_SVC"].as(int()) == 1 && c["MENU_CODE_VAT"].as(int()) == 1) {	/*부가세*/
		foo.m_vat = (c["MENU_CODE_RATE"].as(int()) - (int)(c["MENU_CODE_RATE"].as(int()) / 12.1)) /10;
	}
	else if(c["MENU_CODE_VAT"].as(int()) == 1) {
		foo.m_vat = c["MENU_CODE_RATE"].as(int()) / 10;
	}
	else { foo.m_vat = c["MENU_CODE_VAT"].as(int()); }
  	foo.m_adv = c["MENU_CODE_ADV"].as(int());
  	foo.m_net = c["MENU_CODE_RATE"].as(int()) - foo.m_vat - foo.m_svc;
  	foo.m_tot = c["MENU_CODE_TOT"].as(int());
	foo.m_credit = 0;
  	foo.m_dc = 0;
  	
   	T.commit();
	
	// TreeView에 등록된 값과 비교
	type_children children = m_refListStore->children();
	if(children.size() !=0)	/* 등록된 데이타가 있다면 */
	{
		int count = 0;	/* for문의 종료값 */
		for(type_children::iterator iter = children.begin(); iter != children.end(); ++iter)
		{
			Gtk::TreeModel::Row row = *iter;
			
			if(row[m_columns.code] == foo.m_code) {
				sales.vatbuff = row[m_columns.vat] / row[m_columns.number]; 
				sales.svcbuff = row[m_columns.svc] / row[m_columns.number]; 
				row[m_columns.number] = row[m_columns.number] +1;
				row[m_columns.price] = sep_thousands(row[m_columns.rate] * row[m_columns.number]);
				row[m_columns.vat] = row[m_columns.vat] + sales.vatbuff;
				row[m_columns.svc] = row[m_columns.svc] + sales.svcbuff; 
				row[m_columns.net] = row[m_columns.rate]*row[m_columns.number] - row[m_columns.svc] - row[m_columns.vat];
				break;
			}
			count++;	
		}
		// 끝까지 비교해서 같은 값이 없다면 추가
		if(count == children.size()) { 
			liststore_add_item(foo); 
		}
	}
	else { liststore_add_item(foo); }   /* 등록된 데이타가 없다면 한줄 추가 */
	price_info();
}

// 메뉴 하단 버튼 클릭
void MainWindow::on_Button_Menu_Low_clicked(Glib::ustring data)
{
	cout << data << endl;
	if( data.compare("현금입금") == 0 )
	{
		table_show(0);
	}
	else if( data.compare("수량") == 0 )
	{
		cntwindow_show();
	}
	else if( data.compare("주문완료") == 0 )
	{
		type_children children = m_refListStore->children();
		if(children.size() ==0)	/* 등록된 데이타가 없다면 */
		{
			msgwindow_show("메뉴를 선택하세요.");
		}
		else {
			if(menustatus.compare("normal") == 0) {
				// 체크인
				cheksave();
				// 영수증 정보
				billinsert();
			}
			if(menustatus.compare("menuadd") == 0) {
				work T(C, "warrning");
				SQL = "DELETE FROM P_CASH_TRAN WHERE \
					CASH_TRAN_SOSOK	='"+sales.SOSOK+"' \
					AND CASH_TRAN_UJCD	='"+sales.Ujcd+"' \
					AND CASH_TRAN_DATE	='"+sales.Workdate+"' \
					AND CASH_TRAN_BILL	='"+sales.nowBILL+"' \
					AND CASH_TRAN_ACNO	='"+sales.nowACCT+"'";
				result R( T.exec(SQL) );
				T.commit();
			}
			// 메뉴정보
			cashsave();
			// 방문객수 갱신
			sales.Total_Person_Func();
			// 테이블화면으로 이동
			menustatus = "normal";
			tablestatus= "normal";
			table_show(0);
		}
	}
	else if( data.compare("취소") == 0 )
	{
		//treeview_init();
		//sales.nowACCT = "-1";
		tablestatus = "normal";
		menustatus = "normal";
		table_show(0);
	}
	else if( data.compare("메뉴찾기") == 0 )
	{
		menufindwindow->show();
	}
	else 
	{
		menu_show(data);
	}
}

//******************************************************************************
//
//		계산 화면 관련 함수
//
//*****************************************************************************
void MainWindow::pay_show()
{
	sales.CHKOUTPRICE = m_Label_Price_info[5]->get_label();
	if( windowstatus.compare("menu") == 0)
	{
		msgwindow_show("주문완료후 계산하세요.");
	}
	else if( windowstatus.compare("pay") != 0 )
	{
		initbutton();
		Gdk::Color color;
		color.set("#990066");
													
		int count = 0;  // 활성 테이블 수 카운트
		for(int j = 0; j < 8; j++)	// 세로 8개
		{
			 for(int i = 0; i < 5; i++)	// 가로 5개
			{	
				if(count < 14)	// CARD 종류
				{
					m_Button_Pay[i][j] = Gtk::manage(new Gtk::Button(sales.card_name[count]));
					m_Button_Pay[i][j]->modify_bg(Gtk::STATE_ACTIVE, color);
					m_Button_Pay[i][j]->modify_bg(Gtk::STATE_NORMAL, color);
					m_Button_Pay[i][j]->set_size_request(118, 70);
					m_Button_Pay[i][j]->show();
					m_Table->attach(*m_Button_Pay[i][j], i, i + 1, j, j + 1);
					// 시그널 이벤트 설정부분 (라벨값 전달)
					m_Button_Pay[i][j]->signal_clicked().connect( sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &MainWindow::on_Button_Pay_clicked ), m_Button_Pay[i][j]->get_label() ) );
				}
				else
				{
					m_Button_Pay[i][j] = Gtk::manage(new Gtk::Button(""));
					m_Button_Pay[i][j]->set_size_request(118, 70);
					m_Button_Pay[i][j]->show();
					m_Table->attach(*m_Button_Pay[i][j], i, i + 1, j, j + 1);
				}
				count++;
			}
		}
		pay_low_show();
	}
  	windowstatus = "pay";
}

// 계산 관련 하단 버튼 생성
void MainWindow::pay_low_show()
{	
	initlowbutton();
	Gdk::Color color;
	color.set("#993399");
	// 하단 버튼 네임 설정 변수
	string low_button_name[2][5] = {"CASH", "CARD", "후불", "접대", "", \
									"영수증출력\n(체크아웃)", "자동DC", "중간정산", "취소", "DC" };

  	for(int j = 0; j < 2; j++)	// 세로 2개
  	{
    	 for(int i = 0; i < 5; i++)	// 가로 5개
     	{ 
   			m_Button_Pay_Low[i][j] = Gtk::manage(new Gtk::Button(low_button_name[j][i]));
        	m_Button_Pay_Low[i][j]->modify_bg(Gtk::STATE_ACTIVE, color);
			m_Button_Pay_Low[i][j]->modify_bg(Gtk::STATE_NORMAL, color);
   			m_Button_Pay_Low[i][j]->set_size_request(30, 30);
   			m_Button_Pay_Low[i][j]->show();
  			m_Table_Low[j]->attach(*m_Button_Pay_Low[i][j], i, i+1, j, j+1);
   			// 시그널 이벤트 설정부분 (라벨값 전달)
   			m_Button_Pay_Low[i][j]->signal_clicked().connect( sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &MainWindow::on_Button_Pay_Low_clicked ), m_Button_Pay_Low[i][j]->get_label() ) );
   		}
   	}
}
//******************************************************************************
//		계산 화면 관련 시그널 이벤트 함수
//******************************************************************************
// 계산버튼클릭
void MainWindow::on_Button_Pay_clicked(Glib::ustring data)
{
	cout << data << "\n" ;
	if( data.compare("계산") == 0 ) { 
		if(sales.nowACCT.compare("0") != 0)
			pay_show();
	}
	else
	{
		cardwindow_show(data);
	}
}
// 계산 하단 버튼 클릭
void MainWindow::on_Button_Pay_Low_clicked(Glib::ustring data)
{
	cout << data << "\n" ;
	if( data.compare("취소") == 0 )
	{
		tablestatus = "normal";
		menustatus = "normal";
		//treeview_init();
		table_show(0);
	}
	else if( data.compare("CASH") == 0 )
	{
		cashwindow_show();
	}
	else if( data.compare("CARD") == 0 )
	{
		//cardwindow_show("NULL");
	}
	else if( data.compare("영수증출력\n(체크아웃)") == 0 )
	{
		if( atoi(m_Label_Price_info[7]->get_label().data()) > 0) {
			msgwindow_show("발란스가 맞지 않아\n체크아웃 할수 없습니다.");
		}
		else { 
			PRINT();
			chekout(); 
			treeview_init();
			table_show(1);
		}
	}
}

//******************************************************************************
//
//		손님당 주문 상품에 대한 관련 함수(TreeView)
//
//*****************************************************************************
// 상단 하단 Treeview 초기화 함수
void MainWindow::treeview_init()
{
	m_refListStore->clear();
	m_refListStore_Detail->clear();	
	price_info();
	sales.nowACCT = "0";
}
// 상품정보 TreeView에 저장
void MainWindow::liststore_add_item(const CellItem_Product& foo)
{
  	Gtk::TreeRow row = *(m_refListStore->append());
	row[m_columns.number]   = foo.m_number;
  	row[m_columns.product]  = foo.m_product;
  	row[m_columns.price]  = foo.m_price;
  	row[m_columns.code]  = foo.m_code;
  	row[m_columns.div]  = foo.m_div;
  	row[m_columns.svc]  = foo.m_svc;
  	row[m_columns.vat]  = foo.m_vat;
  	row[m_columns.adv]  = foo.m_adv;
  	row[m_columns.rate]  = foo.m_rate;
  	row[m_columns.net]  = foo.m_net;
  	row[m_columns.tot]  = foo.m_tot; 	  	
  	row[m_columns.dc]  = foo.m_dc;
	row[m_columns.credit]  = foo.m_credit;
}
//******************************************************************************
//		Treeview 시그널 이벤트 함수
//******************************************************************************
// 트리뷰 메뉴 추가 버튼
void MainWindow::on_Button_Menu_add_clicked()
{
	if(windowstatus.compare("menu") == 0)
	{
		Glib::RefPtr<Gtk::TreeView::Selection> refSelection = m_TreeView_Menu->get_selection();
		refSelection->set_mode(Gtk::SELECTION_SINGLE);
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter)
		{
			Gtk::TreeModel::Row row = *iter;
			sales.vatbuff = row[m_columns.vat] / row[m_columns.number]; 
			sales.svcbuff = row[m_columns.svc] / row[m_columns.number]; 
			row[m_columns.number]  = row[m_columns.number] + 1;
			if(row[m_columns.number] == 0) { m_refListStore->erase(iter); }
			else {
				row[m_columns.price] = sep_thousands(row[m_columns.rate] * row[m_columns.number]); /*총금액*/
				row[m_columns.vat] = row[m_columns.vat] + sales.vatbuff;
				row[m_columns.svc] = row[m_columns.svc] + sales.svcbuff; 
				row[m_columns.net] = row[m_columns.rate]*row[m_columns.number] - row[m_columns.svc] - row[m_columns.vat];
			}
		}	
		price_info();
	}
}

// 트리뷰 메뉴 삭제 버튼
void MainWindow::on_Button_Menu_remove_clicked()
{
	if(windowstatus.compare("menu") == 0)
	{
		Glib::RefPtr<Gtk::TreeSelection> refSelection = m_TreeView_Menu->get_selection();

		if(const Gtk::TreeModel::iterator iter = refSelection->get_selected())
		{
			Gtk::TreeModel::Row row = *iter;
			sales.vatbuff = row[m_columns.vat] / row[m_columns.number]; 
			sales.svcbuff = row[m_columns.svc] / row[m_columns.number]; 
			row[m_columns.number]  = row[m_columns.number] - 1;
			if(row[m_columns.number] == 0) { m_refListStore->erase(iter); }
			else {
				row[m_columns.price]  = sep_thousands(row[m_columns.rate] * row[m_columns.number]);
				row[m_columns.vat] = row[m_columns.vat] - sales.vatbuff;
				row[m_columns.svc] = row[m_columns.svc] - sales.svcbuff; 
				row[m_columns.net] = row[m_columns.rate]* row[m_columns.number] - row[m_columns.svc] - row[m_columns.vat];
			}
		}
		price_info();
	}
}
// 트리뷰 메뉴 위로 올리기 버튼
void MainWindow::on_Button_Menu_up_clicked()
{
	type_children children = m_refListStore->children();
	int treeview_page = children.size() / 8;
	// 페이지 구분
	if(treeview_page != 0)
	{
		// 작동
	}
}
// 트리뷰 메뉴 아래로 내리기 버튼
void MainWindow::on_Button_Menu_down_clicked()
{
	Glib::RefPtr<Gtk::TreeSelection> refSelection = m_TreeView_Menu->get_selection();
	type_children children = m_refListStore->children();
	int treeview_page = children.size() / 8;
	// 페이지 구분
	if(treeview_page != 0)
	{
		//refSelection->signal_changed().connect( sigc::mem_fun(*this, &MainWindow::on_selection_changed));
	}

}
// 자세히 버튼
void MainWindow::on_Button_Detail_clicked()
{
	menudetailwindow->show();
}

//******************************************************************************
//		세부 금액란 라벨 생성 함수
//******************************************************************************
void MainWindow::price_info()
{	
	int price_row[8] = {0,0,0,0,0,0,0,0};
	type_children children = m_refListStore->children();
	for(type_children::iterator iter = children.begin(); iter != children.end(); ++iter)
	{
		Gtk::TreeModel::Row row = *iter;
		if(row)
			price_row[0] += row[m_columns.rate] * row[m_columns.number];	/*총금액*/
			price_row[1] += row[m_columns.dc]; 								/*DC*/
			price_row[2] += row[m_columns.net];  							/*판매액*/
			price_row[3] += row[m_columns.svc];  							/*봉사료*/
			price_row[4] += row[m_columns.vat];  							/*부가세*/
			price_row[5] += row[m_columns.rate] * row[m_columns.number];  	/*청구액*/
			price_row[6] += row[m_columns.credit];							/*입금*/
			price_row[7] += row[m_columns.rate] * row[m_columns.number] - row[m_columns.credit];  	/*잔액*/
	}

	for(int i = 0; i < 8; i++)
	{
		if(m_Label_Price_info[i] != NULL) 
		{
			m_Label_Price_info[i]->hide();
			delete m_Label_Price_info[i];
		}
		m_Label_Price_info[i] = Gtk::manage( new Gtk::Label(sep_thousands(price_row[i])));
		m_Label_Price_info[i]->set_size_request(90, 24);
		m_Label_Price_info[i]->show();
		
		if(i < 5) m_Table_Price_L->attach(*m_Label_Price_info[i], 0, 1, i , i+1);
		else m_Table_Price_R->attach(*m_Label_Price_info[i], 0, 1, i , i+1);
	}
}

//******************************************************************************
//		빌넘버 손님 번호 값 구하기
//******************************************************************************
// 손님 번호
void MainWindow::acctnum()
{

	work T(C, "acctnum");
	SQL = " SELECT \
				MAX(CHEK_TRAN_ACNO) + 1 \
				FROM P_CHEK_TRAN \
			WHERE \
				CHEK_TRAN_SOSOK= '"+sales.SOSOK+"'\
				AND CHEK_TRAN_UJCD= '"+sales.Ujcd+"'\
				AND CHEK_TRAN_DATE= '"+sales.Workdate+"'";
	result R( T.exec(SQL) );
	result::const_iterator c = R.begin();
	if(c[0].as(string()).compare("") == 0) {
		sales.maxACCT = "1";
	}
	else {
		sales.maxACCT = c[0].as(string());
	}
	T.commit();
}
// 빌넘버
void MainWindow::billnum()
{
	work T(C, "billnum");
	SQL = " SELECT \
				MAX(CASH_BILL_BILL) + 1 \
			FROM P_CASH_BILL \
			WHERE \
				CASH_BILL_SOSOK	= '"+sales.SOSOK+"'\
				AND CASH_BILL_UJCD = '"+sales.Ujcd+"'\
				AND CASH_BILL_DATE	='"+sales.Workdate+"'";
	result R( T.exec(SQL) );
	result::const_iterator c = R.begin();
	if(c[0].as(string()).compare("") == 0) {
		sales.maxBILL = "1";
	}
	else {		
		sales.maxBILL = c[0].as(string());
	}
	T.commit();
}
//******************************************************************************
//
//		체크인 체크아웃 관련 함수
//
//******************************************************************************
// 영수증 정보 저장
void MainWindow::billinsert()
{	
	work T(C, "billinsert");
	SQL= " INSERT INTO P_CASH_BILL \
				(CASH_BILL_SOSOK, CASH_BILL_UJCD,   CASH_BILL_DATE, \
				 CASH_BILL_BILL,  CASH_BILL_ACNO,   CASH_BILL_FDATE, \
				 CASH_BILL_FUSER, CASH_BILL_LDATE,  CASH_BILL_LUSER) \
			VALUES ('" +sales.SOSOK+ "','" +sales.Ujcd+ "','"+sales.Workdate+"', \
					'" +sales.nowBILL+ "','" +sales.nowACCT+ "',NOW(), \
					'" +sales.User+ "',NULL,'')";
	result R( T.exec(SQL) );
	T.commit();
}
// 체크인 저장하기
void MainWindow::cheksave()
{
	work T(C, "cheksave");
	SQL= " INSERT INTO P_CHEK_TRAN \
				(CHEK_TRAN_SOSOK, CHEK_TRAN_UJCD,   CHEK_TRAN_DATE, \
				 CHEK_TRAN_ACNO,  CHEK_TRAN_TBNO,   CHEK_TRAN_PERS, \
				 CHEK_TRAN_CTYPE, CHEK_TRAN_STAU,  CHEK_TRAN_YUSER, \
				 CHEK_TRAN_BUSER, CHEK_TRAN_MEMO,  CHEK_TRAN_FDATE, \
				 CHEK_TRAN_FUSER, CHEK_TRAN_LDATE, CHEK_TRAN_LUSER) \
			VALUES ('" +sales.SOSOK+ "','" +sales.Ujcd+ "','"+sales.Workdate+"', \
					'" +sales.nowACCT+ "','" +sales.nowtablnum+ "','1', \
					'0080010002','"+sales.CHKINCODE+"','" +sales.User+ "', \
					'" +sales.User+ "','',NOW(), \
					'" +sales.User+ "',NULL,'')";
	result R( T.exec(SQL) );
	T.commit();
}
// 주문 메뉴 저장
void MainWindow::cashsave()
{
	int count = 1;
	work T(C, "cashsave");
	type_children children = m_refListStore->children();
	for(type_children::iterator iter = children.begin(); iter != children.end(); ++iter)
	{
		Gtk::TreeModel::Row row = *iter;
		if(row)
		{
			// 숫자 -> 문자열 변환
			char seq[10];  
			sprintf(seq, "%d", count);
			ostringstream menu;
			ostringstream amt;
			ostringstream cnt; 
			ostringstream dc;
			ostringstream net;
			ostringstream svc;
			ostringstream vat;
			ostringstream adv;
			ostringstream gtot;
			ostringstream credit;

			string menu_str = "0";
			string amt_str = "0";
			string cnt_str = "0";
			string dc_str = "0";
			string net_str = "0";
			string svc_str = "0";
			string vat_str = "0";
			string adv_str = "0";
			string gtot_str = "0";
			string credit_str = credit.str();
			
			if(row[m_columns.code] == 0) {
				menu_str = "0";
				amt_str = "0";
				cnt_str = "0";
				dc_str = "0";
				net_str = "0";
				svc_str = "0";
				vat_str = "0";
				adv_str = "0";
				gtot_str = "0";
				credit << row[m_columns.credit];
				credit_str = credit.str();
			}
			else {
				menu << row[m_columns.code];	/*메뉴코드*/
				menu_str = menu.str();	
				amt << row[m_columns.rate];		/*단가?*/
				amt_str = amt.str();
				cnt << row[m_columns.number];	/*수량*/
				cnt_str = cnt.str();
				dc << row[m_columns.dc];		/*D/C*/
				dc_str = dc.str();	
				net << row[m_columns.net] / row[m_columns.number];		/*회사수입*/
				net_str = net.str();
				svc << row[m_columns.svc] / row[m_columns.number];		/*봉사료*/
				svc_str = svc.str();
				vat << row[m_columns.vat] / row[m_columns.number];		/*부가세*/
				vat_str = vat.str();
				adv << row[m_columns.adv];		/*ADV*/
				adv_str = adv.str();
				gtot << row[m_columns.rate] * row[m_columns.number];		/*합계*/
				gtot_str = gtot.str();
				credit_str = "0";
			}
			SQL = " INSERT INTO P_CASH_TRAN \
						(CASH_TRAN_SOSOK, CASH_TRAN_UJCD,   CASH_TRAN_DATE, \
						 CASH_TRAN_ACNO,  CASH_TRAN_SEQ,    CASH_TRAN_BILL, \
						 CASH_TRAN_TBNO,  CASH_TRAN_MENU,   CASH_TRAN_DETA, \
						 CASH_TRAN_CARD,  CASH_TRAN_CALC,   CASH_TRAN_SDIV, \
						 CASH_TRAN_VDIV,  CASH_TRAN_ADIV,   CASH_TRAN_BASE_AMT, \
						 CASH_TRAN_CNT,   CASH_TRAN_DC_PER,	CASH_TRAN_DC_AMT, \
						 CASH_TRAN_AMT,	  CASH_TRAN_NET,	CASH_TRAN_SVC, \
						 CASH_TRAN_VAT,	  CASH_TRAN_ADV,	CASH_TRAN_TOT, \
						 CASH_TRAN_GTOT,  CASH_TRAN_CHARGE,	CASH_TRAN_CREDIT, \
						 CASH_TRAN_VOID_STAU,  CASH_TRAN_VOID_SEQ,   CASH_TRAN_VOID_MEMO, \
						 CASH_TRAN_TRAN_STAU,  CASH_TRAN_TRAN_ACNO1, CASH_TRAN_TRAN_SEQ1, \
						 CASH_TRAN_TRAN_ACNO2, CASH_TRAN_TRAN_SEQ2,  CASH_TRAN_JUBDAE1, \
						 CASH_TRAN_JUBDAE2,	   CASH_TRAN_FDATE,		 CASH_TRAN_FUSER, \
						 CASH_TRAN_LDATE,	   CASH_TRAN_LUSER) \
					VALUES ('" +sales.SOSOK+ "','" +sales.Ujcd+ "','"+sales.Workdate+"', \
							'" +sales.nowACCT+ "','" +seq+ "','" +sales.nowBILL+ "', \
							'" +sales.nowtablnum+ "','" +menu_str+ "','" +row[m_columns.product]+ "', \
							'','f','f', \
							'f','f','" +amt_str+ "', \
							'" +cnt_str+ "',0,'" +dc_str+ "', \
							'" +amt_str+ "','" +net_str+ "','" +svc_str+ "', \
							'" +vat_str+ "','" +adv_str+ "','" +amt_str+ "', \
							'" +gtot_str+ "','" +gtot_str+ "'," +credit_str+ ", \
							'" +/*VOID_STAU*/+ "',0,'" +/*VOID_MEMO*/+ "', \
							'" +/*TRAN_STAU*/+ "',0,0, \
							'0',0,'" +/*JUBDAE1*/+ "', \
							'" +/*JUBDAE2*/+ "',NOW(),'" +sales.User+ "', \
							NULL,'')";

			result R( T.exec(SQL) );
			count++;
		}	/* if */
	}	/* for	*/
	T.commit();
}
// 체크아웃
void MainWindow::chekout()
{
	// acct 넘버 필요
	work T(C, "chekout");
	SQL= "  UPDATE P_CHEK_TRAN SET \
				CHEK_TRAN_STAU='" +sales.CHKOUTCODE+ "', \
				CHEK_TRAN_LDATE=NOW(), \
				CHEK_TRAN_LUSER='" +sales.User+ "' \
			WHERE \
				CHEK_TRAN_SOSOK	='" +sales.SOSOK+ "' \
				AND CHEK_TRAN_UJCD	='" +sales.Ujcd+ "' \
				AND CHEK_TRAN_DATE	='"+sales.Workdate+"' \
				AND CHEK_TRAN_ACNO	='" +sales.nowACCT+ "'";
	result R( T.exec(SQL) );
	T.commit();
	sales.Total_Price_Func();
	CASHDRAWER();
	LINEDISPLAY();
	//PRINT();
}
//******************************************************************************
//		테이블 이체
//******************************************************************************
void MainWindow::tabletranswindow_init()
{
	m_refGlade->get_widget("tabletranswindow", tabletranswindow);
	m_refGlade->get_widget("tabletranstreeview", tabletranstreeview);
	tabletranstreeview->set_model(m_refListStore);
	tabletranstreeview->append_column("항  목", m_columns.product);	
   	tabletranstreeview->append_column("수  량", m_columns.number);
  	tabletranstreeview->append_column("금  액", m_columns.price);
	m_refGlade->connect_clicked("Trans_Button_Enter", sigc::mem_fun(*this, &MainWindow::on_Trans_Button_Enter_clicked));
	m_refGlade->connect_clicked("Trans_Button_Cancel", sigc::mem_fun(*this, &MainWindow::on_Trans_Button_Cancel_clicked));
	m_refGlade->connect_clicked("Trans_Button_Allselect", sigc::mem_fun(*this, &MainWindow::on_Trans_Button_Allselect_clicked));
	m_refGlade->connect_clicked("Trans_Button_Allunselect", sigc::mem_fun(*this, &MainWindow::on_Trans_Button_Allunselect_clicked));
	Glib::RefPtr<Gtk::TreeView::Selection> refSelectiontabletrans = tabletranstreeview->get_selection();
	refSelectiontabletrans->set_mode(Gtk::SELECTION_MULTIPLE);
	refSelectiontabletrans->selected_foreach_iter( sigc::mem_fun(*this, &MainWindow::selected_row_callback) );
	refSelectiontabletrans->set_select_function( sigc::mem_fun(*this, &MainWindow::select_function) );
}
void MainWindow::selected_row_callback(const Gtk::TreeModel::iterator& iter)
{
	Gtk::TreeModel::Row row = *iter;
	//Do something with the row.
}
bool MainWindow::select_function(const Glib::RefPtr<Gtk::TreeModel>& model, const Gtk::TreeModel::Path& path, bool)
{
  const Gtk::TreeModel::iterator iter = model->get_iter(path);
  return iter->children().empty(); // only allow leaf nodes to be selected
}

void MainWindow::on_Trans_Button_Enter_clicked()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelectiontabletrans = tabletranstreeview->get_selection();
	refSelectiontabletrans->set_mode(Gtk::SELECTION_MULTIPLE);
	Gtk::TreeModel::iterator iter = refSelectiontabletrans->get_selected();
	if(iter)
	{
		Gtk::TreeModel::Row row = *iter;
		// 각 테이블 시퀀스 최대값 구하기
		std::string nowSEQ, transSEQ;
		work T(C, "tabletrans");
		SQL = " SELECT \
					MAX(CASH_TRAN_SEQ) + 1 \
					FROM P_CASH_TRAN \
				WHERE \
					CASH_TRAN_SOSOK	='"+sales.SOSOK+"' \
				AND CASH_TRAN_UJCD	='"+sales.Ujcd+"' \
				AND CASH_TRAN_DATE	='"+sales.Workdate+"' \
				AND CASH_TRAN_ACNO	='"+sales.nowACCT+"'";
		result R( T.exec(SQL) );
		result::const_iterator c = R.begin();
		nowSEQ = c[0].as(string());
		
		
		SQL = " SELECT \
					MAX(CASH_TRAN_SEQ) + 1 \
					FROM P_CASH_TRAN \
				WHERE \
					CASH_TRAN_SOSOK	='"+sales.SOSOK+"' \
				AND CASH_TRAN_UJCD	='"+sales.Ujcd+"' \
				AND CASH_TRAN_DATE	='"+sales.Workdate+"' \
				AND CASH_TRAN_ACNO	='"+sales.transACCT+"'";
		result R2( T.exec(SQL) );
		result::const_iterator c2 = R2.begin();
		transSEQ = c2[0].as(string());
		
		// int -> string
		ostringstream menu;
		ostringstream amt;
		ostringstream cnt; 
		ostringstream dc;
		ostringstream net;
		ostringstream svc;
		ostringstream vat;
		ostringstream adv;
		ostringstream tot;
		menu << row[m_columns.code];
		string menu_str = menu.str();
		amt << row[m_columns.price];
		string amt_str = amt.str();
		cnt << row[m_columns.number];
		string cnt_str = cnt.str();
		dc << row[m_columns.dc];
		string dc_str = dc.str();
		net << row[m_columns.net];
		string net_str = net.str();
		svc << row[m_columns.svc];
		string svc_str = svc.str();
		vat << row[m_columns.vat];
		string vat_str = vat.str();
		adv << row[m_columns.adv];
		string adv_str = adv.str();
		tot << row[m_columns.tot];
		string tot_str = tot.str();
		// 이체할 테이블 - 로 표기	
		SQL = " INSERT INTO P_CASH_TRAN \
					(CASH_TRAN_SOSOK, CASH_TRAN_UJCD,   CASH_TRAN_DATE, \
					 CASH_TRAN_ACNO,  CASH_TRAN_SEQ,    CASH_TRAN_BILL, \
					 CASH_TRAN_TBNO,  CASH_TRAN_MENU,   CASH_TRAN_DETA, \
					 CASH_TRAN_CARD,  CASH_TRAN_CALC,   CASH_TRAN_SDIV, \
					 CASH_TRAN_VDIV,  CASH_TRAN_ADIV,   CASH_TRAN_BASE_AMT, \
					 CASH_TRAN_CNT,   CASH_TRAN_DC_PER,	CASH_TRAN_DC_AMT, \
					 CASH_TRAN_AMT,	  CASH_TRAN_NET,	CASH_TRAN_SVC, \
					 CASH_TRAN_VAT,	  CASH_TRAN_ADV,	CASH_TRAN_TOT, \
					 CASH_TRAN_GTOT,  CASH_TRAN_CHARGE,	CASH_TRAN_CREDIT, \
					 CASH_TRAN_VOID_STAU,  CASH_TRAN_VOID_SEQ,   CASH_TRAN_VOID_MEMO, \
					 CASH_TRAN_TRAN_STAU,  CASH_TRAN_TRAN_ACNO1, CASH_TRAN_TRAN_SEQ1, \
					 CASH_TRAN_TRAN_ACNO2, CASH_TRAN_TRAN_SEQ2,  CASH_TRAN_JUBDAE1, \
					 CASH_TRAN_JUBDAE2,	   CASH_TRAN_FDATE,		 CASH_TRAN_FUSER, \
					 CASH_TRAN_LDATE,	   CASH_TRAN_LUSER) \
				VALUES ('" +sales.SOSOK+ "','" +sales.Ujcd+ "','"+sales.Workdate+"', \
						'" +sales.nowACCT+ "','" +nowSEQ+ "','" +sales.nowBILL+ "', \
						'" +sales.nowtablnum+ "','" +menu_str+ "','" +row[m_columns.product]+ "', \
						'','f','f', \
						'f','f','-" +amt_str+ "', \
						'-" +cnt_str+ "',0,'-" +dc_str+ "', \
						'-" +amt_str+ "','-" +net_str+ "','-" +svc_str+ "', \
						'-" +vat_str+ "','-" +adv_str+ "','-" +amt_str+ "', \
						'-" +tot_str+ "','-" +tot_str+ "',0, \
						'" +/*VOID_STAU*/+ "',0,'" +/*VOID_MEMO*/+ "', \
						'" +/*TRAN_STAU*/+ "',0,0, \
						'0',0,'" +/*JUBDAE1*/+ "', \
						'" +/*JUBDAE2*/+ "',NOW(),'" +sales.User+ "', \
						NULL,'')";
		result R3( T.exec(SQL) );	
		SQL = " INSERT INTO P_CASH_TRAN \
					(CASH_TRAN_SOSOK, CASH_TRAN_UJCD,   CASH_TRAN_DATE, \
					 CASH_TRAN_ACNO,  CASH_TRAN_SEQ,    CASH_TRAN_BILL, \
					 CASH_TRAN_TBNO,  CASH_TRAN_MENU,   CASH_TRAN_DETA, \
					 CASH_TRAN_CARD,  CASH_TRAN_CALC,   CASH_TRAN_SDIV, \
					 CASH_TRAN_VDIV,  CASH_TRAN_ADIV,   CASH_TRAN_BASE_AMT, \
					 CASH_TRAN_CNT,   CASH_TRAN_DC_PER,	CASH_TRAN_DC_AMT, \
					 CASH_TRAN_AMT,	  CASH_TRAN_NET,	CASH_TRAN_SVC, \
					 CASH_TRAN_VAT,	  CASH_TRAN_ADV,	CASH_TRAN_TOT, \
					 CASH_TRAN_GTOT,  CASH_TRAN_CHARGE,	CASH_TRAN_CREDIT, \
					 CASH_TRAN_VOID_STAU,  CASH_TRAN_VOID_SEQ,   CASH_TRAN_VOID_MEMO, \
					 CASH_TRAN_TRAN_STAU,  CASH_TRAN_TRAN_ACNO1, CASH_TRAN_TRAN_SEQ1, \
					 CASH_TRAN_TRAN_ACNO2, CASH_TRAN_TRAN_SEQ2,  CASH_TRAN_JUBDAE1, \
					 CASH_TRAN_JUBDAE2,	   CASH_TRAN_FDATE,		 CASH_TRAN_FUSER, \
					 CASH_TRAN_LDATE,	   CASH_TRAN_LUSER) \
				VALUES ('" +sales.SOSOK+ "','" +sales.Ujcd+ "','"+sales.Workdate+"', \
						'" +sales.transACCT+ "','" +transSEQ+ "','" +sales.transBILL+ "', \
						'" +sales.transtablnum+ "','" +menu_str+ "','" +row[m_columns.product]+ "', \
						'','f','f', \
						'f','f','" +amt_str+ "', \
						'" +cnt_str+ "',0,'" +dc_str+ "', \
						'" +amt_str+ "','" +net_str+ "','" +svc_str+ "', \
						'" +vat_str+ "','" +adv_str+ "','" +amt_str+ "', \
						'" +tot_str+ "','" +tot_str+ "',0, \
						'" +/*VOID_STAU*/+ "',0,'" +/*VOID_MEMO*/+ "', \
						'" +/*TRAN_STAU*/+ "',0,0, \
						'0',0,'" +/*JUBDAE1*/+ "', \
						'" +/*JUBDAE2*/+ "',NOW(),'" +sales.User+ "', \
						NULL,'')";
		result R4( T.exec(SQL) );
		T.commit();
		cout << "선택테이블:" << sales.nowtablnum << "acct:" << sales.nowACCT << "SEQ:" << nowSEQ << endl;
		cout << "받는테이블:" << sales.transtablnum << "acct:" << sales.transACCT << "SEQ:" << transSEQ << endl;
		tabletranswindow->hide();
		treeview_init();
		table_show(0);
		tablestatus = "normal";
	}				
	else { msgwindow_show("항목을 선택하세요"); }
	// 선택 테이블, acno, bill 비교 메뉴 시퀀스 +1 메뉴 저장
	// 옮길 테이블메뉴, acno, bill 시퀀스 +1 메뉴 -표기 저장
	// 트리뷰에서 선택된 값이 넘어가도록 한다.	
	price_info();
}
void MainWindow::on_Trans_Button_Cancel_clicked()
{
	tabletranswindow->hide();
	tablestatus = "normal";
}
void MainWindow::on_Trans_Button_Allselect_clicked()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelectiontabletrans = tabletranstreeview->get_selection();
	refSelectiontabletrans->select_all();
}
void MainWindow::on_Trans_Button_Allunselect_clicked()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelectiontabletrans = tabletranstreeview->get_selection();
	refSelectiontabletrans->unselect_all();
}

//******************************************************************************
//		메뉴찾기
//******************************************************************************
void MainWindow::menufindwindow_init()
{
	m_refGlade->get_widget("menufindwindow", menufindwindow);
	m_refGlade->get_widget("menufindtreeview", menufindtreeview);
	m_refGlade->connect_clicked("MenuFind_Button_Cancel", sigc::mem_fun(*this, &MainWindow::on_MenuFind_Button_Cancel_clicked));
}
void MainWindow::on_MenuFind_Button_Cancel_clicked()
{
	menufindwindow->hide();
}
//******************************************************************************
//		마감
//******************************************************************************
void MainWindow::workclosewindow_init()
{
	m_refGlade->get_widget("workclosewindow", workclosewindow);
	m_pMainWindowMap->connect_widget("WorkClose_Entry_User", m_WorkClose_Entry_User);
	m_pMainWindowMap->connect_widget("WorkClose_Entry_Ujname", m_WorkClose_Entry_Ujname);
	m_pMainWindowMap->connect_widget("WorkClose_Entry_Workdate", m_WorkClose_Entry_Workdate);
	m_refGlade->connect_clicked("WorkClose_Button_Enter", sigc::mem_fun(*this, &MainWindow::on_WorkClose_Button_Enter_clicked));
	m_refGlade->connect_clicked("WorkClose_Button_Cancel", sigc::mem_fun(*this, &MainWindow::on_WorkClose_Button_Cancel_clicked));
	m_WorkClose_Entry_User = sales.User;
	m_WorkClose_Entry_Ujname = sales.Ujname;
	m_WorkClose_Entry_Workdate = sales.Workdate;
}
void MainWindow::on_WorkClose_Button_Enter_clicked()
{
	workclosewindow->hide();
	work T(C, "workclose1");
	SQL = " SELECT \
				COUNT(CHEK_TRAN_ACNO) \
				FROM P_CHEK_TRAN \
			WHERE \
				CHEK_TRAN_SOSOK	='"+sales.SOSOK+"' \
			AND CHEK_TRAN_UJCD	='"+sales.Ujcd+"' \
			AND CHEK_TRAN_DATE	='"+sales.Workdate+"' \
			AND CHEK_TRAN_STAU  ='"+sales.CHKINCODE+"'";
	result R( T.exec(SQL) );
	result::const_iterator c = R.begin();
	if(c[0].as(int()) > 0) {
		msgwindow_show("밸런스가 맞지 않아 마감할수 없습니다.");
		cout << c[0].as(int()) << endl;
	}
	else if(c[0].as(int()) == 0)
	{
		ostringstream buf;
		buf << atoi(sales.Workdate.data()) + 1;
		std::string Nextdate = buf.str();
		cout << Nextdate << endl;
		SQL= "	UPDATE P_UJCD_CODE SET \
					UJCD_CODE_DATE = '"+Nextdate+"', \
					UJCD_CODE_LDATE= NOW(), \
					UJCD_CODE_LUSER= '"+sales.User+"' \
				WHERE \
						UJCD_CODE_SOSOK	= '"+sales.SOSOK+"' \
					AND UJCD_CODE_UJCD  = '"+sales.Ujcd+"' \
					AND UJCD_CODE_DATE	= '"+sales.Workdate+"'";
		result R2( T.exec(SQL) );
		SQL= "	INSERT INTO P_CLOSE_LOG \
					(CLOSE_LOG_SOSOK,CLOSE_LOG_DATE,CLOSE_LOG_UJCD, \
					CLOSE_LOG_FUSER,CLOSE_LOG_FDATE, \
					CLOSE_LOG_LUSER,CLOSE_LOG_LDATE) \
				VALUES ('"+sales.SOSOK+"','"+sales.Workdate+"','"+sales.Ujcd+"','"+sales.User+"',NOW(),NULL,NULL)";

		result R3( T.exec(SQL) );
		sales.Workdate = Nextdate;
		m_Label_Workdate->set_text(sales.Workdate);
		msgwindow_show("마감을 완료 하였습니다.");
		sales.Total_Person_Func();
		sales.Total_Price_Func();
	}
	T.commit();
}
void MainWindow::on_WorkClose_Button_Cancel_clicked()
{
	workclosewindow->hide();
}
//******************************************************************************
// 		종료 버튼 & 타이머 함수
//******************************************************************************
void MainWindow::on_Button_Exit_clicked()
{
	hide();
}
// 타이머 함수 
bool MainWindow::onSecondElapsed(void)
{
    Glib::RefPtr<Gdk::Window> win = get_window();
    if (win)
    {	 
    	sales.Timer();
   	    m_Label_Workdate->set_text(sales.Workdate);
	    m_Label_Time->set_text(sales.Time);
		m_Label_Total_Person->set_text(sales.Total_Person);
	    m_Label_Total_Price->set_text(sales.Total_Price);
		// CallerID
		int dev;
		int retval = 0;
		cid_ctrl_info ctrl_info;
		dev = open( "/dev/cid0", O_RDWR|O_NDELAY);
		if(dev >= 0) {
			//printf("Test program started.\n");
			printf("Reading CID from  ...\n");
			memset( &ctrl_info, 0, sizeof(cid_ctrl_info) );
			ctrl_info.pipe = 1;

			retval = ioctl(dev, IOCTL_EZUSB_READ_CID, &ctrl_info);
			if(retval == 0) {
				if( strlen(ctrl_info.buff) ) {
					msgwindow_show(ctrl_info.buff);
					//system("date");
					printf("Port %d: %s\n\n", ctrl_info.pipe, ctrl_info.buff);
				}
			}
			if( ctrl_info.pipe == 4 ) ctrl_info.pipe = 1;
			else ctrl_info.pipe++;
		}
		//else printf("Error: can't open device file!\n");
		}
		return true;
}

/******************************************************************************/
//
//		CASHWINDOW
//
/******************************************************************************/
// 버튼 엔트리 초기화
void MainWindow::cashwindow_init()
{
	m_refGlade->get_widget("cash_window", cashwindow);
	m_pMainWindowMap->connect_widget("Cash_Entry_remain", m_Cash_Entry_remain);
	m_pMainWindowMap->connect_widget("Cash_Entry_get", m_Cash_Entry_get);
	m_pMainWindowMap->connect_widget("Cash_Entry_give", m_Cash_Entry_give);
	m_refGlade->connect_clicked("Cash_Button_Cancel", sigc::mem_fun(*this, &MainWindow::on_Cash_Button_Cancel_clicked));
	m_refGlade->connect_clicked("Cash_Button_Enter", sigc::mem_fun(*this, &MainWindow::on_Cash_Button_Enter_clicked));
	m_refGlade->connect_clicked("Cash_Button_Clear", sigc::mem_fun(*this, &MainWindow::on_Cash_Button_Clear_clicked));	
	// 숫자 패드 이벤트 생성
	for(int i=0; i<=12; i++)
	{	
		char numpad_name[20];
		sprintf(numpad_name, "Cash_Button_Numpad_%d", i);
		m_refGlade->connect_clicked(numpad_name, sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &MainWindow::on_Cash_Button_Numpad_clicked ), numpad_name));
	}
}
// 현금 계산화면 보여주기
void MainWindow::cashwindow_show()
{
	// 엔트리값 초기화
	m_Cash_Entry_remain.clear();
	m_Cash_Entry_get.clear();
	m_Cash_Entry_give.clear();
	getnumber.str("");
	
	// 실제 값 초기화
	Cash_Remain = 0;
	Cash_Give = 0;
	Cash_Get = 0;
	type_children children = m_refListStore->children();
	for(type_children::iterator iter = children.begin(); iter != children.end(); ++iter)
	{
		Gtk::TreeModel::Row row = *iter;
		if(row)
			Cash_Remain += row[m_columns.rate] * row[m_columns.number] - row[m_columns.credit];  	/*잔액*/
	}

   	m_Cash_Entry_remain = sep_thousands(Cash_Remain);
	// 윈도우 값 갱신
	if(m_pMainWindowMap)
		m_pMainWindowMap->transfer_variables_to_widgets();
	cashwindow->show();
}
void MainWindow::on_Cash_Button_Numpad_clicked(Glib::ustring data)
{
	// 받은 값을 get엔트리에 쌓아넣는다.
	// 숫자 패드 for 13개 해서 값을 넣기
	for(int i=0; i<=13; i++)
	{	
		char numpad_name[20];
		sprintf(numpad_name, "Cash_Button_Numpad_%d", i);
		if(data.compare(numpad_name) == 0 && i <10)
		{
			getnumber << i;
		}
		else if(data.compare(numpad_name) == 0 && i>9 && getnumber.str().compare("") !=0)
		{
			if(i==10) getnumber << "00";
			else if(i==11) getnumber << "000";
			else if(i==12) getnumber << "0000";
		}
	}
	
	// 거스름돈 계산
	Cash_Get = atoi(getnumber.str().data());
	m_Cash_Entry_get = sep_thousands(Cash_Get);
	Cash_Give = Cash_Remain - Cash_Get;
	m_Cash_Entry_give = sep_thousands(Cash_Give);
	m_pMainWindowMap->transfer_variables_to_widgets();
}
void MainWindow::on_Cash_Button_Enter_clicked()
{
	// 받은돈 > 청구액 이면 영수증 출력후 테이블 화면으로 이동

	if(Cash_Get == 0) {
		m_Cash_Entry_get = "금액을 입력하세요.";
		m_pMainWindowMap->transfer_variables_to_widgets();
		m_Cash_Entry_get.clear();
	}
	else {
		m_pMainWindowMap->transfer_variables_to_widgets();
		std::string nowSEQ;
		work T(C, "cashadd");
		SQL = " SELECT \
					MAX(CASH_TRAN_SEQ) + 1 \
					FROM P_CASH_TRAN \
				WHERE \
					CASH_TRAN_SOSOK	='"+sales.SOSOK+"' \
				AND CASH_TRAN_UJCD	='"+sales.Ujcd+"' \
				AND CASH_TRAN_DATE	='"+sales.Workdate+"' \
				AND CASH_TRAN_ACNO	='"+sales.nowACCT+"' \
				AND CASH_TRAN_BILL	='"+sales.nowBILL+"'";
		result R( T.exec(SQL) );
		result::const_iterator c = R.begin();
		nowSEQ = c[0].as(string());
		cout << nowSEQ << endl;
		//입금으로 저장 받은돈 문자열로 변경
		ostringstream Cash_Get_buf;
		Cash_Get_buf << Cash_Get;
		string Cash_Get_str = Cash_Get_buf.str();
		SQL = " INSERT INTO P_CASH_TRAN \
					(CASH_TRAN_SOSOK, CASH_TRAN_UJCD,   CASH_TRAN_DATE, \
					 CASH_TRAN_ACNO,  CASH_TRAN_SEQ,    CASH_TRAN_BILL, \
					 CASH_TRAN_TBNO,  CASH_TRAN_MENU,   CASH_TRAN_DETA, \
					 CASH_TRAN_CARD,  CASH_TRAN_CALC,   CASH_TRAN_SDIV, \
					 CASH_TRAN_VDIV,  CASH_TRAN_ADIV,   CASH_TRAN_BASE_AMT, \
					 CASH_TRAN_CNT,   CASH_TRAN_DC_PER,	CASH_TRAN_DC_AMT, \
					 CASH_TRAN_AMT,	  CASH_TRAN_NET,	CASH_TRAN_SVC, \
					 CASH_TRAN_VAT,	  CASH_TRAN_ADV,	CASH_TRAN_TOT, \
					 CASH_TRAN_GTOT,  CASH_TRAN_CHARGE,	CASH_TRAN_CREDIT, \
					 CASH_TRAN_VOID_STAU,  CASH_TRAN_VOID_SEQ,   CASH_TRAN_VOID_MEMO, \
					 CASH_TRAN_TRAN_STAU,  CASH_TRAN_TRAN_ACNO1, CASH_TRAN_TRAN_SEQ1, \
					 CASH_TRAN_TRAN_ACNO2, CASH_TRAN_TRAN_SEQ2,  CASH_TRAN_JUBDAE1, \
					 CASH_TRAN_JUBDAE2,	   CASH_TRAN_FDATE,		 CASH_TRAN_FUSER, \
					 CASH_TRAN_LDATE,	   CASH_TRAN_LUSER) \
				VALUES ('" +sales.SOSOK+ "','" +sales.Ujcd+ "','"+sales.Workdate+"', \
						'" +sales.nowACCT+ "','" +nowSEQ+ "','" +sales.nowBILL+ "', \
						'" +sales.nowtablnum+ "','0','현금', \
						'','f','f', \
						'f','f','0', \
						'0','0','0', \
						'0','0','0', \
						'0','0','0', \
						'0','0',"+Cash_Get_str+", \
						'" +/*VOID_STAU*/+ "',0,'" +/*VOID_MEMO*/+ "', \
						'" +/*TRAN_STAU*/+ "',0,0, \
						'0',0,'" +/*JUBDAE1*/+ "', \
						'" +/*JUBDAE2*/+ "',NOW(),'" +sales.User+ "', \
						NULL,'')";
		result R2( T.exec(SQL) );
		T.commit();
		CellItem_Product foo;
		foo.m_code = 0;
		foo.m_number = 0;
		foo.m_product = "현금";
		foo.m_price = sep_thousands(Cash_Get);
		foo.m_div = 0;
		foo.m_svc = 0;
		foo.m_vat = 0;
		foo.m_adv = 0;
		foo.m_rate = 0;
		foo.m_net = 0;
		foo.m_tot = 0;
		foo.m_dc = 0;
		foo.m_credit = Cash_Get;
		liststore_add_item(foo);
		price_info();
		cashwindow->hide();
	}
}
void MainWindow::on_Cash_Button_Clear_clicked()
{
	m_Cash_Entry_get.clear();
	m_Cash_Entry_give.clear();
	getnumber.str("");
	m_pMainWindowMap->transfer_variables_to_widgets();
}
void MainWindow::on_Cash_Button_Cancel_clicked()
{
	cashwindow->hide();
}
/******************************************************************************/
//
//		CARDWINDOW
//
/******************************************************************************/
// 버튼 엔트리 초기화
void MainWindow::cardwindow_init()
{	
	m_refGlade->get_widget("card_window", cardwindow);
	m_pMainWindowMap->connect_widget("Card_Entry_cardname", m_Card_Entry_cardname);
	m_refGlade->get_widget("Card_Entry_number", m_Card_Entry_number);
	m_pMainWindowMap->connect_widget("Card_Entry_price", m_Card_Entry_price);
	m_refGlade->connect_clicked("Card_Button_Cancel", sigc::mem_fun(*this, &MainWindow::on_Card_Button_Cancel_clicked));
	m_refGlade->connect_clicked("Card_Button_Enter", sigc::mem_fun(*this, &MainWindow::on_Card_Button_Enter_clicked));
	m_refGlade->connect_clicked("Card_Button_Clear", sigc::mem_fun(*this, &MainWindow::on_Card_Button_Clear_clicked));	
	// 숫자 패드 이벤트 생성
	for(int i=0; i<=12; i++)
	{	
		char numpad_name[20];
		sprintf(numpad_name, "Card_Button_Numpad_%d", i);
		m_refGlade->connect_clicked(numpad_name, sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &MainWindow::on_Card_Button_Numpad_clicked ), numpad_name));
	}
	show_all_children();
}
void MainWindow::cardwindow_show(Glib::ustring cardname)
{
	// 초기화
	m_Card_Entry_cardname = cardname;
	m_Card_Entry_number->set_text("");
	m_Card_Entry_price.clear();
	getnumber.str("");
	Card_Remain = 0;
	Card_Get = 0;
	// 청구액 엔트리에 삽입
	type_children children = m_refListStore->children();
	for(type_children::iterator iter = children.begin(); iter != children.end(); ++iter)
	{
		Gtk::TreeModel::Row row = *iter;
		if(row)
			Card_Remain += row[m_columns.rate] * row[m_columns.number] - row[m_columns.credit];  	/*잔액*/
	}
	Card_Get = Card_Remain;
   	m_Card_Entry_price = sep_thousands(Card_Remain);
	// 윈도우 값 갱신
	if(m_pMainWindowMap)
		m_pMainWindowMap->transfer_variables_to_widgets();
	cardwindow->show();
}
void MainWindow::on_Card_Button_Numpad_clicked(Glib::ustring data)
{
	for(int i=0; i<=13; i++)
	{	
		char numpad_name[20];
		sprintf(numpad_name, "Card_Button_Numpad_%d", i);
		if(data.compare(numpad_name) == 0 && i <10)
		{
			getnumber << i;
		}
		else if(data.compare(numpad_name) == 0 && i>9 && getnumber.str().compare("") !=0)
		{
			if(i==10) getnumber << "00";
			else if(i==11) getnumber << "000";
			else if(i==12) getnumber << "0000";
		}
	}
	Card_Get = atoi(getnumber.str().data());
	m_Card_Entry_price = sep_thousands(Card_Get);
	m_pMainWindowMap->transfer_variables_to_widgets();
}
void MainWindow::on_Card_Button_Enter_clicked()
{	
	if(m_pMainWindowMap)
		m_pMainWindowMap->transfer_variables_to_widgets();
	if(m_Card_Entry_number->get_text().compare("") == 0) {
		msgwindow_show("입력된 카드번호가 없습니다.");
	}
	else if(m_Card_Entry_price.compare("") == 0) { 
		msgwindow_show("금액을 입력하세요.");
	}
	else {
		std::string nowSEQ;
		work T(C, "Cardadd");
		SQL = " SELECT \
					MAX(CASH_TRAN_SEQ) + 1 \
					FROM P_CASH_TRAN \
				WHERE \
					CASH_TRAN_SOSOK	='"+sales.SOSOK+"' \
				AND CASH_TRAN_UJCD	='"+sales.Ujcd+"' \
				AND CASH_TRAN_DATE	='"+sales.Workdate+"' \
				AND CASH_TRAN_ACNO	='"+sales.nowACCT+"' \
				AND CASH_TRAN_BILL	='"+sales.nowBILL+"'";
		result R( T.exec(SQL) );
		result::const_iterator c = R.begin();
		nowSEQ = c[0].as(string());
		cout << nowSEQ << endl;
		//입금으로 저장 받은돈 문자열로 변경
		ostringstream Card_Get_buf;
		Card_Get_buf << Card_Get;
		string Card_Get_str = Card_Get_buf.str();
		SQL = " INSERT INTO P_CASH_TRAN \
					(CASH_TRAN_SOSOK, CASH_TRAN_UJCD,   CASH_TRAN_DATE, \
					 CASH_TRAN_ACNO,  CASH_TRAN_SEQ,    CASH_TRAN_BILL, \
					 CASH_TRAN_TBNO,  CASH_TRAN_MENU,   CASH_TRAN_DETA, \
					 CASH_TRAN_CARD,  CASH_TRAN_CALC,   CASH_TRAN_SDIV, \
					 CASH_TRAN_VDIV,  CASH_TRAN_ADIV,   CASH_TRAN_BASE_AMT, \
					 CASH_TRAN_CNT,   CASH_TRAN_DC_PER,	CASH_TRAN_DC_AMT, \
					 CASH_TRAN_AMT,	  CASH_TRAN_NET,	CASH_TRAN_SVC, \
					 CASH_TRAN_VAT,	  CASH_TRAN_ADV,	CASH_TRAN_TOT, \
					 CASH_TRAN_GTOT,  CASH_TRAN_CHARGE,	CASH_TRAN_CREDIT, \
					 CASH_TRAN_VOID_STAU,  CASH_TRAN_VOID_SEQ,   CASH_TRAN_VOID_MEMO, \
					 CASH_TRAN_TRAN_STAU,  CASH_TRAN_TRAN_ACNO1, CASH_TRAN_TRAN_SEQ1, \
					 CASH_TRAN_TRAN_ACNO2, CASH_TRAN_TRAN_SEQ2,  CASH_TRAN_JUBDAE1, \
					 CASH_TRAN_JUBDAE2,	   CASH_TRAN_FDATE,		 CASH_TRAN_FUSER, \
					 CASH_TRAN_LDATE,	   CASH_TRAN_LUSER) \
				VALUES ('" +sales.SOSOK+ "','" +sales.Ujcd+ "','"+sales.Workdate+"', \
						'" +sales.nowACCT+ "','" +nowSEQ+ "','" +sales.nowBILL+ "', \
						'" +sales.nowtablnum+ "','0','" +m_Card_Entry_cardname+ "', \
						'" +m_Card_Entry_number->get_text()+ "','f','f', \
						'f','f','0', \
						'0','0','0', \
						'0','0','0', \
						'0','0','0', \
						'0','0',"+Card_Get_str+", \
						'" +/*VOID_STAU*/+ "',0,'" +/*VOID_MEMO*/+ "', \
						'" +/*TRAN_STAU*/+ "',0,0, \
						'0',0,'" +/*JUBDAE1*/+ "', \
						'" +/*JUBDAE2*/+ "',NOW(),'" +sales.User+ "', \
						NULL,'')";
		result R2( T.exec(SQL) );
		T.commit();
		CellItem_Product foo;
		foo.m_code = 0;
		foo.m_number = 0;
		foo.m_product = m_Card_Entry_cardname;
		foo.m_price = sep_thousands(Card_Get);
		foo.m_div = 0;
		foo.m_svc = 0;
		foo.m_vat = 0;
		foo.m_adv = 0;
		foo.m_rate = 0;
		foo.m_net = 0;
		foo.m_tot = 0;
		foo.m_dc = 0;
		foo.m_credit = Card_Get;
		liststore_add_item(foo);
		price_info();
		cardwindow->hide();
	}
}
void MainWindow::on_Card_Button_Clear_clicked()
{
	m_Card_Entry_price.clear();
	m_Card_Entry_number->set_text("");
	getnumber.str("");
	m_pMainWindowMap->transfer_variables_to_widgets();
}
void MainWindow::on_Card_Button_Cancel_clicked()
{
	cardwindow->hide();
}

/******************************************************************************/
//
//		CNTWINDOW
//
/******************************************************************************/
// 버튼 엔트리 초기화
void MainWindow::cntwindow_init()
{	
	m_refGlade->get_widget("cnt_window", cntwindow);
	m_pMainWindowMap->connect_widget("Cnt_Entry", m_Cnt_Entry);
	m_refGlade->connect_clicked("Cnt_Button_Cancel", sigc::mem_fun(*this, &MainWindow::on_Cnt_Button_Cancel_clicked));
	m_refGlade->connect_clicked("Cnt_Button_Enter", sigc::mem_fun(*this, &MainWindow::on_Cnt_Button_Enter_clicked));
	m_refGlade->connect_clicked("Cnt_Button_Clear", sigc::mem_fun(*this, &MainWindow::on_Cnt_Button_Clear_clicked));	
	// 숫자 패드 이벤트 생성
	for(int i=0; i<=12; i++)
	{	
		char numpad_name[20];
		sprintf(numpad_name, "Cnt_Button_Numpad_%d", i);
		m_refGlade->connect_clicked(numpad_name, sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &MainWindow::on_Cnt_Button_Numpad_clicked ), numpad_name));
	}
}
void MainWindow::cntwindow_show()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = m_TreeView_Menu->get_selection();
	refSelection->set_mode(Gtk::SELECTION_SINGLE);
	Gtk::TreeModel::iterator iter = refSelection->get_selected();
	if(!iter) {
		msgwindow_show("수량을 변경할 메뉴를 선택하세요");
	}
	else {
		m_Cnt_Entry.clear();	
		getnumber.str("0");
		// 윈도우 값 갱신
		if(m_pMainWindowMap)
			m_pMainWindowMap->transfer_variables_to_widgets();
		cntwindow->show();
	}
}
void MainWindow::on_Cnt_Button_Numpad_clicked(Glib::ustring data)
{
	for(int i=0; i<=13; i++)
	{	
		char numpad_name[20];
		sprintf(numpad_name, "Cnt_Button_Numpad_%d", i);
		if(data.compare(numpad_name) == 0 && i <10)
		{
			getnumber << i;
		}
		else if(data.compare(numpad_name) == 0 && i>9 && getnumber.str().compare("") !=0)
		{
			if(i==10) getnumber << "00";
			else if(i==11) getnumber << "000";
			else if(i==12) getnumber << "0000";
		}
	}
	m_Cnt_Entry = getnumber.str();
	m_pMainWindowMap->transfer_variables_to_widgets();
}
void MainWindow::on_Cnt_Button_Enter_clicked()
{
	if(m_Cnt_Entry.substr(0,1).compare("0") == 0 || m_Cnt_Entry.compare("") == 0)
	{
		msgwindow_show("수량을 잘못 입력하셨습니다.");
	}
	else {
		Glib::RefPtr<Gtk::TreeView::Selection> refSelection = m_TreeView_Menu->get_selection();
		refSelection->set_mode(Gtk::SELECTION_SINGLE);
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter)
		{
			Gtk::TreeModel::Row row = *iter;
			sales.vatbuff = row[m_columns.vat] / row[m_columns.number]; 
			sales.svcbuff = row[m_columns.svc] / row[m_columns.number]; 
			row[m_columns.number]  = atoi(m_Cnt_Entry.data());
			row[m_columns.price] = sep_thousands(row[m_columns.rate] * row[m_columns.number]); /*총금액*/
			row[m_columns.vat] = sales.vatbuff * row[m_columns.number];
			row[m_columns.svc] = sales.svcbuff * row[m_columns.number]; 
			row[m_columns.net] = row[m_columns.rate]* row[m_columns.number] - row[m_columns.svc] - row[m_columns.vat];
		}
		price_info();
		cntwindow->hide();
	}
}
void MainWindow::on_Cnt_Button_Clear_clicked()
{
	m_Cnt_Entry.clear();
	getnumber.str("");
	m_pMainWindowMap->transfer_variables_to_widgets();
}
void MainWindow::on_Cnt_Button_Cancel_clicked()
{
	cntwindow->hide();
}
//******************************************************************************
//		빌 찾기
//******************************************************************************
void MainWindow::billfindwindow_init()
{
	m_refGlade->get_widget("billfindwindow", billfindwindow);
	m_pMainWindowMap->connect_widget("BillFind_Entry", m_BillFind_Entry);
	m_refGlade->connect_clicked("BillFind_Button_Cancel", sigc::mem_fun(*this, &MainWindow::on_BillFind_Button_Cancel_clicked));
	m_refGlade->connect_clicked("BillFind_Button_Enter", sigc::mem_fun(*this, &MainWindow::on_BillFind_Button_Enter_clicked));
	m_refGlade->connect_clicked("BillFind_Button_Clear", sigc::mem_fun(*this, &MainWindow::on_BillFind_Button_Clear_clicked));	
	// 숫자 패드 이벤트 생성
	for(int i=0; i<=12; i++)
	{	
		char numpad_name[20];
		sprintf(numpad_name, "BillFind_Button_Numpad_%d", i);
		m_refGlade->connect_clicked(numpad_name, sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &MainWindow::on_BillFind_Button_Numpad_clicked ), numpad_name));
	}

}
void MainWindow::on_BillFind_Button_Numpad_clicked(Glib::ustring data)
{
	// 받은 값을 get엔트리에 쌓아넣는다.
	// 숫자 패드 for 13개 해서 값을 넣기
	for(int i=0; i<=13; i++)
	{	
		char numpad_name[20];
		sprintf(numpad_name, "BillFind_Button_Numpad_%d", i);
		if(data.compare(numpad_name) == 0 && i <10)
		{
			getnumber << i;
		}
		else if(data.compare(numpad_name) == 0 && i>9 && getnumber.str().compare("") !=0)
		{
			if(i==10) getnumber << "00";
			else if(i==11) getnumber << "000";
			else if(i==12) getnumber << "0000";
		}
	}
	m_BillFind_Entry = getnumber.str();
	m_pMainWindowMap->transfer_variables_to_widgets();
}
void MainWindow::on_BillFind_Button_Enter_clicked()
{
	if(m_BillFind_Entry.substr(0,1).compare("0") == 0 || m_BillFind_Entry.compare("") == 0)
	{
		msgwindow_show("빌번호를 잘못 입력하셧습니다.");
	}
	else {
		billwindow_show(m_BillFind_Entry);
		billfindwindow->hide();
	}
}
void MainWindow::on_BillFind_Button_Clear_clicked()
{
	m_BillFind_Entry.clear();
	getnumber.str("");
	m_pMainWindowMap->transfer_variables_to_widgets();
}
void MainWindow::on_BillFind_Button_Cancel_clicked()
{
	billfindwindow->hide();
}
//******************************************************************************
//		빌 정보
//******************************************************************************
void MainWindow::billwindow_init()
{
	m_refGlade->get_widget("bill_window", billwindow);
	m_refGlade->get_widget("billmenutreeview", billmenutreeview);
	m_refGlade->get_widget("billdetailtreeview", billdetailtreeview);
	m_refGlade->connect_clicked("Bill_Button_Void", sigc::mem_fun(*this, &MainWindow::on_Bill_Button_Void_clicked));
	m_refGlade->connect_clicked("Bill_Button_Rebill", sigc::mem_fun(*this, &MainWindow::on_Bill_Button_Rebill_clicked));
	m_refGlade->connect_clicked("Bill_Button_Close", sigc::mem_fun(*this, &MainWindow::on_Bill_Button_Close_clicked));	
	m_refGlade->connect_clicked("Bill_Button_Detail", sigc::mem_fun(*this, &MainWindow::on_Bill_Button_Detail_clicked));	

}
void MainWindow::billwindow_show(Glib::ustring bill)
{
	cout << bill << endl;
	billwindow->show();
}
void MainWindow::on_Bill_Button_Void_clicked()
{
	voidmemo->show();
}
void MainWindow::on_Bill_Button_Rebill_clicked()
{
	printstatus = "rebill";
}
void MainWindow::on_Bill_Button_Close_clicked()
{
	billwindow->hide();
}
void MainWindow::on_Bill_Button_Detail_clicked()
{
	
}
//******  Void Memo	 **********************************************************/
void MainWindow::voidmemo_init()
{
	m_refGlade->get_widget("voidmemo", voidmemo);
	m_pMainWindowMap->connect_widget("Voidmemo_Entry", m_Voidmemo_Entry);
	m_refGlade->connect_clicked("Voidmemo_Button_Enter", sigc::mem_fun(*this, &MainWindow::on_Voidmemo_Button_Enter_clicked));
	m_refGlade->connect_clicked("Voidmemo_Button_Cancel", sigc::mem_fun(*this, &MainWindow::on_Voidmemo_Button_Cancel_clicked));
}
void MainWindow::on_Voidmemo_Button_Enter_clicked()
{
  	if(m_pMainWindowMap)
    	m_pMainWindowMap->transfer_widgets_to_variables();

	if(m_Voidmemo_Entry.compare("") == 0) {
		msgwindow_show("Void 사유를 필히 입력하세요");
	}
	else { 
		cout << m_Voidmemo_Entry << endl;
		//DB저장 and Void처리
		voidmemo->hide();
	}
}
void MainWindow::on_Voidmemo_Button_Cancel_clicked()
{
	voidmemo->hide();
}

//******************************************************************************
//		메세지 윈도우
//******************************************************************************
void MainWindow::msgwindow_init()
{
	m_refGlade->get_widget("msgwindow", msgwindow);
	m_refGlade->get_widget("MSG_Label",m_MSG_Label);
	m_refGlade->connect_clicked("MSG_Button", sigc::mem_fun(*this, &MainWindow::on_MSG_Button_clicked));
}
void MainWindow::msgwindow_show(Glib::ustring msg)
{   
	m_MSG_Label->set_text(msg);
	msgwindow->show();
}
void MainWindow::on_MSG_Button_clicked()
{
	msgwindow->hide();
}
/******************************************************************************/
//
//		자세히 버튼
//
/******************************************************************************/
void MainWindow::menudetailwindow_init()
{
	m_refGlade->get_widget("menudetailwindow", menudetailwindow);
	m_refGlade->get_widget("MenuDetailTreeview", m_MenuDetailTreeview);
	m_MenuDetailTreeview->set_model(m_refListStore);
	m_MenuDetailTreeview->set_rules_hint();
	m_MenuDetailTreeview->append_column("  상품명   ", m_columns.product);
	m_MenuDetailTreeview->append_column("  수량   ", m_columns.number);
   	m_MenuDetailTreeview->append_column("  정산    ", m_columns.div);
	m_MenuDetailTreeview->append_column("  봉사료   ", m_columns.svc);
	m_MenuDetailTreeview->append_column("  부가세   ", m_columns.vat);
  	m_MenuDetailTreeview->append_column("  세금    ", m_columns.adv);
	m_MenuDetailTreeview->append_column("  단 가   ", m_columns.rate);
	m_MenuDetailTreeview->append_column("  D/C   ", m_columns.dc);
	m_MenuDetailTreeview->append_column("  수입금액    ", m_columns.net);
	m_MenuDetailTreeview->append_column("  합계   ", m_columns.tot);
	m_MenuDetailTreeview->append_column("  총계  ", m_columns.price);
	m_refGlade->connect_clicked("MenuDetail_Button_Cancel", sigc::mem_fun(*this, &MainWindow::on_MenuDetail_Button_Cancel_clicked));
}
void MainWindow::on_MenuDetail_Button_Cancel_clicked()
{
	menudetailwindow->hide();
}
/******************************************************************************/
//
//		REPORTWINDOW
//
/******************************************************************************/
void MainWindow::reportwindow_init()
{	
	Gdk::Color color;
	color.set("#000000");
	m_refGlade->get_widget("reportwindow", reportwindow);
	m_refGlade->get_widget("From_Calendar", m_FromdateCalendar);
	m_refGlade->get_widget("To_Calendar", m_TodateCalendar);
	// label
	m_refGlade->get_widget("Report_Label_Menu",m_Report_Label_Menu);
	m_Report_Label_Menu->modify_fg(Gtk::STATE_NORMAL, color);
	m_refGlade->get_widget("Report_Label_Charge",m_Report_Label_Charge);
	m_Report_Label_Charge->modify_fg(Gtk::STATE_NORMAL, color);
	m_refGlade->get_widget("Report_Label_Credit",m_Report_Label_Credit);
	m_Report_Label_Credit->modify_fg(Gtk::STATE_NORMAL, color);	
	// Entry
	m_pMainWindowMap->connect_widget("Report_Entry_Ujcd", m_Report_Entry_Ujcd);
	m_pMainWindowMap->connect_widget("Report_Entry_Fromdate", m_Report_Entry_Fromdate);
	m_pMainWindowMap->connect_widget("Report_Entry_Todate", m_Report_Entry_Todate);
	// Calendar
	m_FromdateCalendar->signal_day_selected().connect(sigc::mem_fun(*this, &MainWindow::From_day_selected));
	m_TodateCalendar->signal_day_selected().connect(sigc::mem_fun(*this, &MainWindow::To_day_selected));
	// Button
	m_refGlade->connect_clicked("Report_Button_Enter", sigc::mem_fun(*this, &MainWindow::on_Report_Button_Enter_clicked));
	m_refGlade->connect_clicked("Report_Button_Print", sigc::mem_fun(*this, &MainWindow::on_Report_Button_Print_clicked));
	m_refGlade->connect_clicked("Report_Button_Cancel", sigc::mem_fun(*this, &MainWindow::on_Report_Button_Cancel_clicked));
	// Treeview
	// 품목현황
	m_refGlade->get_widget("ReportMenuTreeview", m_ReportMenuTreeview);
	m_refListReportMenu = Gtk::ListStore::create(m_report_menu_columns);	
	m_ReportMenuTreeview->set_model(m_refListReportMenu);
	m_ReportMenuTreeview->set_rules_hint();
	m_ReportMenuTreeview->append_column("   메 뉴 명        ", m_report_menu_columns.deta);
  	m_ReportMenuTreeview->append_column("   단가    ", m_report_menu_columns.rate);
	m_ReportMenuTreeview->append_column("   판매금액   ", m_report_menu_columns.tot);
	m_ReportMenuTreeview->append_column("   판매수량   ", m_report_menu_columns.cnt);
	m_ReportMenuTreeview->append_column("   회사수입   ", m_report_menu_columns.net);
	m_ReportMenuTreeview->append_column("   봉 사 료   ", m_report_menu_columns.svc);
	m_ReportMenuTreeview->append_column("   부 가 세   ", m_report_menu_columns.vat);
	m_ReportMenuTreeview->append_column("   합    계    ", m_report_menu_columns.gtot);
	// 판매현황	
	m_refGlade->get_widget("ReportChargeTreeview", m_ReportChargeTreeview);
	m_refListReportCharge = Gtk::ListStore::create(m_report_charge_columns);	
	m_ReportChargeTreeview->set_model(m_refListReportCharge);
	m_ReportChargeTreeview->set_rules_hint();
	m_ReportChargeTreeview->append_column("   영업일자   ", m_report_charge_columns.workdate);
   	m_ReportChargeTreeview->append_column("   빌 번 호   ", m_report_charge_columns.billnum);
	m_ReportChargeTreeview->append_column("   테 이 블   ", m_report_charge_columns.tablnum);
  	m_ReportChargeTreeview->append_column("   메뉴금액    ", m_report_charge_columns.rate);
	m_ReportChargeTreeview->append_column("   DC금액   ", m_report_charge_columns.dc);
	m_ReportChargeTreeview->append_column("   NET   ", m_report_charge_columns.net);	
	m_ReportChargeTreeview->append_column("   봉 사 료   ", m_report_charge_columns.svc);
	m_ReportChargeTreeview->append_column("   부 가 세   ", m_report_charge_columns.vat);
	m_ReportChargeTreeview->append_column("   합    계    ", m_report_charge_columns.gtot);
	// 수입현황
	m_refGlade->get_widget("ReportCreditTreeview", m_ReportCreditTreeview);
	m_refListReportCredit = Gtk::ListStore::create(m_report_credit_columns);	
	m_ReportCreditTreeview->set_model(m_refListReportCredit);
	m_ReportCreditTreeview->set_rules_hint();
	m_ReportCreditTreeview->append_column("   영업일자   ", m_report_credit_columns.workdate);
   	m_ReportCreditTreeview->append_column("   빌 번 호   ", m_report_credit_columns.billnum);
	m_ReportCreditTreeview->append_column("   테 이 블   ", m_report_credit_columns.tablnum);
	m_ReportCreditTreeview->append_column("   현  금       ", m_report_credit_columns.cash);
	m_ReportCreditTreeview->append_column("   카  드       ", m_report_credit_columns.card);
	m_ReportCreditTreeview->append_column("   후  불       ", m_report_credit_columns.later);
	m_ReportCreditTreeview->append_column("   접  대       ", m_report_credit_columns.jubdae);
}
void MainWindow::reportwindow_show()
{
	m_Report_Entry_Fromdate = get_from_date().format_string("%x");
	m_Report_Entry_Todate = get_to_date().format_string("%x");
	m_Report_Entry_Ujcd = sales.Ujname;
	if(m_pMainWindowMap)
		m_pMainWindowMap->transfer_variables_to_widgets();
	reportwindow->show();
}
void MainWindow::on_Report_Button_Cancel_clicked()
{
	reportwindow->hide();
}
void MainWindow::on_Report_Button_Enter_clicked()
{
	m_refListReportMenu->clear();
	m_refListReportCharge->clear();
	m_refListReportCredit->clear();
	std::string Fromdate = "20" + get_from_date().format_string("%y%m%d");
	std::string Todate = "20" + get_to_date().format_string("%y%m%d");
	// 레포트 쿼리문
	work T(C, "report");
	// 품목현황 (메뉴코드로 SUM)
	SQL = " SELECT \
				CASH_TRAN_DETA,	CASH_TRAN_BASE_AMT, SUM(CASH_TRAN_TOT), SUM(CASH_TRAN_CNT), \
				SUM(CASH_TRAN_NET), SUM(CASH_TRAN_SVC), SUM(CASH_TRAN_VAT), SUM(CASH_TRAN_GTOT) \
			FROM P_CASH_TRAN \
			INNER JOIN P_MENU_CODE MENU \
				ON CASH_TRAN_SOSOK=MENU.MENU_CODE_SOSOK \
				AND CASH_TRAN_MENU=MENU.MENU_CODE_CODE \
			WHERE   CASH_TRAN_SOSOK='"+sales.SOSOK+"'\
				AND CASH_TRAN_UJCD='"+sales.Ujcd+"'\
				AND CASH_TRAN_DATE>='"+Fromdate+"' \
				AND CASH_TRAN_DATE<='"+Todate+"' \
			GROUP BY CASH_TRAN_MENU, CASH_TRAN_DETA, CASH_TRAN_BASE_AMT"; 
	result R( T.exec(SQL) );
	int menu_total_cnt=0, menu_total_net=0, menu_total_svc=0, menu_total_vat=0, menu_total_tot=0;
	for (result::const_iterator c = R.begin(); c != R.end(); ++c)
	{			
		Gtk::TreeRow row = *(m_refListReportMenu->append());
		row[m_report_menu_columns.deta] = c["CASH_TRAN_DETA"].as(string());
		row[m_report_menu_columns.rate] = sep_thousands(c["CASH_TRAN_BASE_AMT"].as(int()));
		row[m_report_menu_columns.tot] = sep_thousands(c[2].as(int()));
		row[m_report_menu_columns.cnt] = sep_thousands(c[3].as(int()));
		row[m_report_menu_columns.net] = sep_thousands(c[4].as(int()));
		row[m_report_menu_columns.svc] = sep_thousands(c[5].as(int()));
		row[m_report_menu_columns.vat] = sep_thousands(c[6].as(int()));
		row[m_report_menu_columns.gtot] = sep_thousands(c[7].as(int()));
		menu_total_cnt += c[3].as(int());
		menu_total_net += c[4].as(int());
		menu_total_svc += c[5].as(int());
		menu_total_vat += c[6].as(int());
		menu_total_tot += c[7].as(int());
	}
	Gtk::TreeRow menurow = *(m_refListReportMenu->append());
	menurow[m_report_menu_columns.rate] = "합  계 : ";
	menurow[m_report_menu_columns.cnt] = sep_thousands(menu_total_cnt);
	menurow[m_report_menu_columns.net] = sep_thousands(menu_total_net);
	menurow[m_report_menu_columns.svc] = sep_thousands(menu_total_svc);
	menurow[m_report_menu_columns.vat] = sep_thousands(menu_total_vat);
	menurow[m_report_menu_columns.gtot] = sep_thousands(menu_total_tot);
	// 판매현황 (빌번호로 SUM)
	int charge_total_dc=0, charge_total_net=0, charge_total_svc=0, charge_total_vat=0, charge_total_tot=0;
	SQL = " SELECT \
				CASH_TRAN_DATE, CASH_TRAN_BILL, CASH_TRAN_TBNO, \
				SUM(CASH_TRAN_AMT), SUM(CASH_TRAN_DC_AMT), \
				SUM(CASH_TRAN_NET), SUM(CASH_TRAN_SVC), SUM(CASH_TRAN_VAT) \
			FROM P_CASH_TRAN \
			WHERE   CASH_TRAN_SOSOK='"+sales.SOSOK+"'\
				AND CASH_TRAN_UJCD='"+sales.Ujcd+"'\
				AND CASH_TRAN_MENU>'0' \
				AND CASH_TRAN_DATE>='"+Fromdate+"' \
				AND CASH_TRAN_DATE<='"+Todate+"' \
			GROUP BY CASH_TRAN_BILL, CASH_TRAN_DATE, CASH_TRAN_TBNO \
			ORDER BY CASH_TRAN_DATE DESC";
	result R2( T.exec(SQL) );
	for (result::const_iterator c = R2.begin(); c != R2.end(); ++c)
	{			
		Gtk::TreeRow row = *(m_refListReportCharge->append());
		row[m_report_charge_columns.workdate] = c["CASH_TRAN_DATE"].as(string());
		row[m_report_charge_columns.billnum] = c["CASH_TRAN_BILL"].as(string());
		row[m_report_charge_columns.tablnum] = c["CASH_TRAN_TBNO"].as(string());
		row[m_report_charge_columns.rate] = sep_thousands(c[3].as(int()));	/*메뉴금액합계*/
		row[m_report_charge_columns.dc] = sep_thousands(c[4].as(int()));	/*DC금액합계*/
		row[m_report_charge_columns.net] = sep_thousands(c[5].as(int()));		/*회사수입합계*/
		row[m_report_charge_columns.svc] = sep_thousands(c[6].as(int()));		/*봉사료합계*/
		row[m_report_charge_columns.vat] = sep_thousands(c[7].as(int()));		/*부가세합계*/
		row[m_report_charge_columns.gtot] = sep_thousands(c[3].as(int()));	/*전체합계*/
		charge_total_dc += c[4].as(int());
		charge_total_net += c[5].as(int());
		charge_total_svc += c[6].as(int());
		charge_total_vat += c[7].as(int());
		charge_total_tot += c[3].as(int());
	}
	Gtk::TreeRow chargerow = *(m_refListReportCharge->append());
	chargerow[m_report_charge_columns.tablnum] = "합  계 : ";
	chargerow[m_report_charge_columns.dc] = sep_thousands(charge_total_dc);
	chargerow[m_report_charge_columns.net] = sep_thousands(charge_total_net);
	chargerow[m_report_charge_columns.svc] = sep_thousands(charge_total_svc);
	chargerow[m_report_charge_columns.vat] = sep_thousands(charge_total_vat);
	chargerow[m_report_charge_columns.gtot] = sep_thousands(charge_total_tot);
	// 입금현황 (빌번호로 SUM)
	SQL = " SELECT \
				CASH_TRAN_DATE, CASH_TRAN_BILL, CASH_TRAN_TBNO, \
				SUM(CASH_TRAN_CREDIT) \
			FROM P_CASH_TRAN \
			WHERE   CASH_TRAN_SOSOK='"+sales.SOSOK+"'\
				AND CASH_TRAN_UJCD='"+sales.Ujcd+"'\
				AND CASH_TRAN_MENU='0' \
				AND CASH_TRAN_DATE>='"+Fromdate+"' \
				AND CASH_TRAN_DATE<='"+Todate+"' \
			GROUP BY CASH_TRAN_BILL, CASH_TRAN_DATE, CASH_TRAN_TBNO \
			ORDER BY CASH_TRAN_DATE DESC";
	result R3( T.exec(SQL) );
	for (result::const_iterator c = R3.begin(); c != R3.end(); ++c)
	{			
		Gtk::TreeRow row = *(m_refListReportCredit->append());
		row[m_report_credit_columns.workdate] = c["CASH_TRAN_DATE"].as(string());
		row[m_report_credit_columns.billnum] = c["CASH_TRAN_BILL"].as(string());
		row[m_report_credit_columns.tablnum] = c["CASH_TRAN_TBNO"].as(string());
		row[m_report_credit_columns.cash] = c[3].as(string());	/*현금*/
		row[m_report_credit_columns.card] = c[3].as(string());	/*카드*/
		row[m_report_credit_columns.later] = "0";								/*후불*/
		row[m_report_credit_columns.jubdae] = "0";								/*접대*/
	}
	T.commit();
}
void MainWindow::on_Report_Button_Print_clicked()
{
}
void MainWindow::From_day_selected()
{
	m_Report_Entry_Fromdate = get_from_date().format_string("%x");
	m_pMainWindowMap->transfer_variables_to_widgets();
}
void MainWindow::To_day_selected()
{
	m_Report_Entry_Todate = get_to_date().format_string("%x");
	m_pMainWindowMap->transfer_variables_to_widgets();
}
Glib::Date MainWindow::get_from_date() const
{
	using Glib::Date;
	unsigned int year = 0, month = 0, day = 0;
	m_FromdateCalendar->get_date(year, month, day);
	return Date(Date::Day(day), Date::Month(Date::JANUARY + month), Date::Year(year));
}
Glib::Date MainWindow::get_to_date() const
{
	using Glib::Date;
	unsigned int year = 0, month = 0, day = 0;
	m_TodateCalendar->get_date(year, month, day);
	return Date(Date::Day(day), Date::Month(Date::JANUARY + month), Date::Year(year));
}

//******************************************************************************
//		콤마 삽입함수
//******************************************************************************
std::string MainWindow::sep_thousands(int i)
{
    const char *locale_name =
    #ifdef WINDOWS
    	"korean";
    #else
    	"ko_KR";
    #endif
    
    ostringstream oss;
    oss.imbue(locale(locale_name));
    oss << i;
    return oss.str();
}

//******************************************************************************
//		영수증 출력함수
//******************************************************************************
void MainWindow::PRINT()
{	
	POSPrinter.FontAlign(0);
	POSPrinter.PrintNormal(1,1, "------------------------------------------\n");
	POSPrinter.PrintNormal(1,1, "             ***  영  수  증  ***\n");
	POSPrinter.PrintNormal(1,1, "------------------------------------------\n");
	POSPrinter.PrintNormal(1,1, "주  소 : "+sales.SOSOKADDR+"\n");
	POSPrinter.PrintNormal(1,1, "대표자 : "+sales.SOSOKTNAME+"\n");
	POSPrinter.PrintNormal(1,1, "사업자등록번호 : "+sales.SOSOKSANO+"\n");
	POSPrinter.PrintNormal(1,1, "T E L : "+sales.SOSOKTEL+"\n");
	POSPrinter.PrintNormal(1,1, "F A X : "+sales.SOSOKFAX+"\n");
	POSPrinter.PrintNormal(1,1, "담당자 : "+sales.Username+"\n");
	POSPrinter.PrintNormal(1,1, "------------------------------------------\n");
	POSPrinter.PrintNormal(1,1, "Table# : ");
	POSPrinter.PrintNormal(1,1, sales.nowtablnum);
	POSPrinter.PrintNormal(1,1, "\t");
	POSPrinter.PrintNormal(1,1, "Bill# : ");
	POSPrinter.PrintNormal(1,1, sales.nowBILL);
	POSPrinter.PrintNormal(1,1, "\n");
	POSPrinter.PrintNormal(1,1, sales.Nowdate);
	POSPrinter.PrintNormal(1,1, "     ");
	POSPrinter.PrintNormal(1,1, sales.Time);
	POSPrinter.PrintNormal(1,1, "\n"); 
	POSPrinter.PrintNormal(1,1, "------------------------------------------\n");
	POSPrinter.PrintNormal(1,1, "메뉴             |   단가 |수량|      금액\n");
	POSPrinter.PrintNormal(1,1, "------------------------------------------\n");
	
	type_children children = m_refListStore->children();
	if(children.size() !=0)	
	{
		// 메뉴항목 출력
		for(type_children::iterator iter = children.begin(); iter != children.end(); ++iter)
		{
			Gtk::TreeModel::Row row = *iter;
			if(row[m_columns.code] > 0)
			{
				/* 메뉴 18자리 한글9글자*/
				std::string s1 = ""+row[m_columns.product];
				/* 단가 8자리*/
				std::string s2 = sep_thousands(row[m_columns.rate]);
				/* 수량 4자리*/
				std::string s3 = sep_thousands(row[m_columns.number]);
				/* 금액 12자리*/
				std::string s4 = ""+row[m_columns.price];
				if(s1.size()/3*2 > 18) /* 18자리보다 크면 */
					s1.append("\n                  ");
				else {	/* 18자리보다 작으면 */
					for(int i=s1.size()/3*2+1; i <= 18; i++) {
						s1.push_back(' ');
					}
				}
				if(s2.size() > 8)
					s2.append("\n                          ");
				else {
					for(int i=s2.size()+1; i <= 8; i++) {
						s2 = ' '+s2;
					}
				}
				if(s3.size() > 4)
					s3.append("\n                              ");
				else {
					for(int i=s3.size()+1; i <= 4; i++) {
						s3 = ' '+s3;
					}
				}
				if(s4.size() > 12)
					s4.append("\n                                      ");
				else {
					for(int i=s4.size()+1; i <= 12; i++) {
						s4 = ' '+s4;
					}
				}
				cout << s1 << s2 << s3 << s4 << endl;
				std::string menuprint;
				menuprint = s1 + s2 + s3 + s4;
				cout << menuprint << endl;
				POSPrinter.PrintNormal(1,1, menuprint);
				POSPrinter.PrintNormal(1,1, "\n");
			}
		}
		POSPrinter.PrintNormal(1,1, "          AMOUNT|      SERVICE|        VAT\n");
		std::string amount = m_Label_Price_info[2]->get_label();	/*16자리*/
		std::string service = m_Label_Price_info[3]->get_label();	/*14자리*/
		std::string vat = m_Label_Price_info[4]->get_label();		/*12자리*/
		for(int i=amount.size()+1; i <= 16; i++) {
			amount = ' '+amount;
		}
		for(int i=service.size()+1; i <= 14; i++) {
			service = ' '+service;
		}
		for(int i=vat.size()+1; i <= 12; i++) {
			vat = ' '+vat;
		}
		std::string ASV = amount + service + vat;
		POSPrinter.PrintNormal(1,1, ASV);
		POSPrinter.PrintNormal(1,1, "\n");
		POSPrinter.PrintNormal(1,1, "TOTAL                 ");
		std::string charge = m_Label_Price_info[5]->get_label();		/*20자리*/
		for(int i=charge.size()+1; i <= 20; i++) {
			charge = ' '+charge;
		}
		POSPrinter.PrintNormal(1,1, charge);
		POSPrinter.PrintNormal(1,1, "\n");
		POSPrinter.PrintNormal(1,1, "------------------------------------------\n");
		POSPrinter.PrintNormal(1,1, "입금종류                      |   금 액 \n");
		POSPrinter.PrintNormal(1,1, "------------------------------------------\n");
		for(type_children::iterator iter = children.begin(); iter != children.end(); ++iter)
		{
			Gtk::TreeModel::Row row = *iter;
			if(row[m_columns.code] == 0)
			{
				/* 입금종류 28자리 */
				std::string s1 = ""+row[m_columns.product];
				if(s1.compare("현금") !=0)
					s1 = s1 + " (카드번호 : 1111-xxxx-xxxx)";
				/* 금액 14자리 */
				std::string s2 = ""+row[m_columns.price];
				if(s2.size() > 14)
					s2.append("\n                                      ");
				else {
					for(int i=s2.size()+1; i <= 14; i++) {
						s2 = ' '+s2;
					}
				}
				std::string creditprint;
				creditprint = s1 + "\n                            " + s2; 
				
				cout << s1 << "," << s2 << endl;
				cout << s1.size() << "," << s2.size() << endl;
				POSPrinter.PrintNormal(1,1, creditprint);
				POSPrinter.PrintNormal(1,1, "\n");
			}
		}
	}
	POSPrinter.PrintNormal(1,1, "------------------------------------------\n");
	POSPrinter.PrintNormal(1,1, "SIGN\n\n");
	POSPrinter.PrintNormal(1,1, "------------------------------------------\n");
	POSPrinter.PrintNormal(1,1, "방문해주셔서 진심으로 감사드립니다.\n");
	POSPrinter.PrintNormal(1,1, "\n");
	POSPrinter.PrintNormal(1,1, "\n");
	POSPrinter.PrintNormal(1,1, "\n");
	POSPrinter.PrintNormal(1,1, "\n");
	POSPrinter.PrintNormal(1,1, "\n"); 
	POSPrinter.CutPaper(100);
}
void MainWindow::CASHDRAWER()
{
	CashDrawer.OpenDrawer();
}
void MainWindow::LINEDISPLAY()
{
	//LineDisplay.Open("CP20C2L");
    //LineDisplay.ClearText();
    //LineDisplay.DisplayText("Thank you", 1);
    //LineDisplay.DisplayTextAt(2, 5, sales.CHKOUTPRICE, 1);
	//LineDisplay.Close();
}
